// auto_1.0.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include "pch.h"
#include "class_center.h"
#include "auto_date_time.h"

#include "class_strategy_position.h"
#include "class_strategy_turtle.h"
#include "class_strategy_wave.h"
#include "class_strategy_spread.h"
#include "class_strategy_grid.h"
#include "class_strategy_grid_static.h"
#include "class_strategy_ba.h"
#include "class_strategy_grid_5.h"
#include "class_strategy_energy.h"
#include "class_strategy_adaptive.h"
#include "class_strategy_trend.h"



lambda_pool pool;
class_center a;

using namespace std;

int main(int argc,char* argv[])
{
a.path_str="abcde";

vector<string> opts;
for(int i=1; i<argc;i++)
{
opts.push_back(argv[i]);
}
//
for(int i=0; i<opts.size(); i++)
{
if(opts[i] == "-h" || opts[i] == "--help")
{
cout<<"帮助文档"<<endl;
}
if(opts[i] =="-v" || opts[i] == "--version")
{
cout<<"版本号1.0"<<endl;
}
if((opts[i] =="-l" || opts[i] == "--path") && i + 1 < opts.size())
{
//cout<<"路径"<<endl;
a.path_str = opts[i+1];
}
if((opts[i] =="-b" || opts[i] == "--broker") && i + 1 < opts.size())
{
//cout<<"经纪商id"<<endl;
a.broker_str = opts[i+1];
}
if((opts[i] =="-u" || opts[i] == "--user") && i + 1 < opts.size())
{
//cout<<"用户名"<<endl;
a.user_str = opts[i+1];
}
if((opts[i] =="-p" || opts[i] == "--password") && i + 1 < opts.size())
{
//cout<<"密码"<<endl;
a.password_str = opts[i+1];
}
if((opts[i] =="-c" || opts[i] == "--config") && i + 1 < opts.size())
{
cout<<"读取配置信息"<<endl;
for(auto &v: read_csv(opts[i+1]))
{
if(v.size() == 2 && v[0] == "path")
{
a.path_str=v[1];
}
if(v.size() == 2 && v[0] == "broker")
{
a.broker_str=v[1];
}
if(v.size() == 2 && v[0] == "user")
{
a.user_str=v[1];
}
if(v.size() == 2 && v[0] == "password")

{
a.password_str=v[1];
}
if(v.size() == 2 && v[0] == "userproductinfo")
{
a.userproductinfo_str=v[1];
}
if(v.size() == 2 && v[0] == "authcode")
{
a.authcode_str=v[1];
}
if(v.size() == 2 && v[0] == "md_front")
{
a.md_front_list.insert(v[1]);
}
if(v.size() == 2 && v[0] == "trader_front")
{
a.trader_front_list.insert(v[1]);
}
if(v.size() == 2 && v[0] == "holiday")
{
a.holiday_list.push_back(v[1]);
}
}
}
if((opts[i] =="-r" || opts[i] == "--release") && i + 1 < opts.size())
{
//cout<<"生产环境或者调试环境"<<endl;
if(opts[i+1] == "debug")
{
	a.flag[1] = 24;
cout<<"调试环境"<<endl;
}
else
{
a.flag[1] = 0;
cout<<"生产环境"<<endl;
}
}
if((opts[i] =="-m" || opts[i] == "--message") && i + 1 < opts.size() ) 
{
//cout<<"是否发送消息"<<endl;
if(opts[i+1] == "yes")
{
	a.flag[3] = 1;
cout<<"发送消息"<<endl;
}
}
}


a.run(a.path_str,a.broker_str,a.user_str,a.password_str, a.userproductinfo_str, a.authcode_str);
//


a.log.insert("log","进入主函数");
if(a.flag[3] != 0)
{
a.log.msg(get_date_time() + "\n" + a.user_str + "\n程序启动");
}
// 获取当前时间点
auto now_0 = std::chrono::system_clock::now();
// 获取当前时间点的年月日时分秒毫秒
std::time_t now_c = std::chrono::system_clock::to_time_t(now_0);
tm now_tm = *std::localtime(&now_c);  
//输出
cout<<"启动时间:"<<now_tm.tm_hour<<":"<<now_tm.tm_min<<":"<< now_tm.tm_sec<< " "<< std::chrono::duration_cast<std::chrono::milliseconds>(now_0.time_since_epoch()).count() % 1000 << " "<< 1900 + now_tm.tm_year<< "-"<< 1 + now_tm.tm_mon<< "-"<< now_tm.tm_mday<<endl;


class_strategy_position cccc(a.path_str.c_str());
class_bars data;

//策略
vector<class_strategy_turtle*> cl_turtle;
vector<class_strategy_energy*> cl_energy;
vector<class_strategy_wave*> cl_wave;
vector<class_strategy_spread*> cl_spread;
vector<class_strategy_grid*> cl_grid;
vector<class_strategy_grid_static*> cl_grid_static;
vector<class_strategy_grid_5*> cl_grid_5;
vector<class_strategy_ba*> cl_ba;
vector<class_strategy_adaptive*> cl_adaptive;
vector<class_strategy_trend*> cl_trend;

string str;
while (str != "return")
{
getline(cin, str, '\n');

//帮助
if(str == "help")
{
cout<<"front  显示前置机信息"<<endl
<<"holiday  显示假日信息"<<endl
<<"clbegin  读取策略"<<endl
<<"clend  退出策略"<<endl
<<"px  行情排序"<<endl
<<"status  显示状态"<<endl
<<"zt  查询合约交易状态"<<endl
<<"cxyqqygx  查询银期签约关系"<<endl
<<"cxyhye  查询银行余额"<<endl
<<"ytq  银行转期货"<<endl
<<"qty  期货转银行"<<endl
<<"zj  显示账户资金"<<endl
<<"cxsxf  查询合约手续费率"<<endl
<<"cxbzj  查询合约保证金率"<<endl
<<"hq  查询行情"<<endl
<<"hqs  查询行情批量"<<endl
<<"cps  查询产品批量"<<endl
<<"cc  显示持仓"<<endl
<<"cc0  设置持仓价值"<<endl
<<"cl  显示策略状态"<<endl
<<"cls  清屏"<<endl
<<"bd  报单"<<endl
<<"bd_2  报单两个合约"<<endl
<<"bd_jc  报单价差"<<endl
<<"cd  撤单"<<endl
<<"qc  清仓"<<endl
<<"zdc  期权自对冲"<<endl
<<"xq  行权"<<endl
<<"xg  执行宣告"<<endl
<<"hot_update  更新主力合约"<<endl
<<"hot  更新病显示主力合约"<<endl
<<"hot_q  更新并显示主力期权"<<endl
<<"mrqq  显示末日期权"<<endl
<<"hot_list  查询主力合约"<<endl
<<"ticks_to_bars  读取tick并转换到bar"<<endl
<<"read  读取bar"<<endl
<<"read_txt  读取文本bar"<<endl
<<"write  写出bzr"<<endl
<<"write_txt  写出bar到文本"<<endl
<<"hb  合并"<<endl
<<"hb_all  合并全部"<<endl
<<"0000  合成0000指数合约"<<endl
<<"average  查询均线"<<endl
<<"min  查询最低"<<endl
<<"max  查询最高"<<endl
<<"bzc  查询标准差"<<endl
<<"bdl  查询波动率"<<endl
<<"qqdj  期权定价"<<endl
<<"完毕"<<endl;
}


//提交动态跟踪
if(str == "adaptive")
{
	std::cout<<"请输入合约代码"<<std::endl;
std::string name;
std::cin>>name;
std::cout<<"请一次输入偏移价格和数量"<<std::endl;
double py = 0;
int sl = 0;
std::cin>>py>>sl;
std::string dz = get_date_time() + "_" + std::to_string(a.nrequestid++);
cl_adaptive.push_back(new class_strategy_adaptive(a.nrequestid++, dz, name, py, sl)); 
std::ofstream file(a.path_str + "/configuration/adaptive.txt", std::ios::app);
if(file.is_open())
{
file<<dz<<","<<name<<","<<py<<","<<sl<<"\n";
file.close();
}
a.flag[50] = 1;
cout<<"完毕"<<endl;
}


//读取策略
if(str == "clbegin")
{
if(a.flag[50] ==0)
{
for(auto &v: read_csv(a.path_str+"/configuration/wave.txt" ))
{
cl_wave.push_back(new class_strategy_wave(a.nrequestid++, v[0], atoi(v[1].c_str()), atof(v[2].c_str()), atof(v[3].c_str()), atoi(v[4].c_str())  ) );
}
for(auto &v: read_csv(a.path_str+"/configuration/grid.txt" ))
{
cl_grid.push_back(new class_strategy_grid(a.nrequestid++, v[0], atoi(v[1].c_str()), atof(v[2].c_str()), atoi(v[3].c_str())  ) );
}
for(auto &v: read_csv(a.path_str+"/configuration/grid_static.txt" ))
{
cl_grid_static.push_back(new class_strategy_grid_static(a.nrequestid++, v[0], atof(v[1].c_str()), atof(v[2].c_str()), atoi(v[3].c_str())  ) );
}
for(auto &v: read_csv(a.path_str+"/configuration/grid_5.txt" ))
{
cl_grid_5.push_back(new class_strategy_grid_5(a.nrequestid++, v[0], atof(v[1].c_str())  ) );
}
for(auto &v: read_csv(a.path_str+"/configuration/spread.txt" ))
{
cl_spread.push_back(new class_strategy_spread(a.nrequestid++, v[0], std::atoi(v[1].c_str()), std::atof(v[2].c_str()), std::atoi(v[3].c_str())  ) );
}
for(auto &v: read_csv(a.path_str+"/configuration/turtle.txt" ))
{
cl_turtle.push_back(new class_strategy_turtle(a.nrequestid++, v[0], atoi(v[1].c_str()), atoi(v[2].c_str()), atoi(v[3].c_str()), atoi(v[4].c_str())  ) );
}
for(auto &v: read_csv(a.path_str+"/configuration/energy.txt" ))
{
cl_energy.push_back(new class_strategy_energy(a.nrequestid++, v[0], atoi(v[1].c_str()), atof(v[2].c_str()), atoi(v[3].c_str())  ) );
}
for(auto &v: read_csv(a.path_str+"/configuration/ba.txt" ))
{
cl_ba.push_back(new class_strategy_ba(a.nrequestid++, v[0], atoi(v[1].c_str()), atof(v[2].c_str()), atof(v[3].c_str()), atoi(v[4].c_str())  ) );
}
for(auto &v: read_csv(a.path_str+"/configuration/adaptive.txt" ))
{
cl_adaptive.push_back(new class_strategy_adaptive(a.nrequestid++, v[0], v[1], atof(v[2].c_str()), atoi(v[3].c_str()) ) );
}
for(auto &v: read_csv(a.path_str+"/configuration/trend.txt" ))
{
cl_trend.push_back(new class_strategy_trend(a.nrequestid++, v[0], atoi(v[1].c_str()), atoi(v[2].c_str()) ) );
}
a.flag[50]=1;
}
cout<<"读取策略完成"<<endl;
}


//退出策略
if (str == "clend")
{
a.flag[50]=0;
for (int i = 0; i < cl_turtle.size(); ++i)
{
a.log.insert("log","海归退出"+cl_turtle[i]->name+" 编号"+to_string(i));
delete cl_turtle[i];
}
cl_turtle.clear();

for (int i = 0; i < cl_grid_5.size(); ++i)
{
a.log.insert("log","网格平衡退出"+cl_grid_5[i]->name+" 编号"+to_string(i));
delete cl_grid_5[i];
}
cl_grid_5.clear();

for (int i = 0; i < cl_wave.size(); ++i)
{
a.log.insert("log","震荡退出"+cl_wave[i]->name+" 编号"+to_string(i));
delete cl_wave[i];
}
cl_wave.clear();

for (int i = 0; i < cl_energy.size(); ++i)
{
a.log.insert("log","动能退出"+cl_energy[i]->name+" 编号"+to_string(i));
delete cl_energy[i];
}
cl_energy.clear();

for (int i = 0; i < cl_spread.size(); ++i)
{
a.log.insert("log","价差回归退出"+cl_spread[i]->name+" 编号"+to_string(i));
delete cl_spread[i];
}
cl_spread.clear();

//网格
for(int i=0;i<cl_grid.size();++i)
{
a.log.insert("log","grid退出"+cl_grid[i]->name+" 编号"+to_string(i));
delete cl_grid[i];
}
cl_grid.clear();

//网格静态
for(int i=0;i<cl_grid_static.size();++i)
{
a.log.insert("log","grid_static退出"+cl_grid_static[i]->name+" 编号"+to_string(i));
delete cl_grid_static[i];
}
cl_grid_static.clear();

for (int i = 0; i < cl_ba.size(); ++i)
{
a.log.insert("log","比例退出"+cl_ba[i]->name+" 编号"+to_string(i));
delete cl_ba[i];
}
cl_ba.clear();

for (int i = 0; i < cl_adaptive.size(); ++i)
{
a.log.insert("log","动态止损退出"+cl_adaptive[i]->name+" 编号"+to_string(i));
delete cl_adaptive[i];
}
cl_adaptive.clear();

for (int i = 0; i < cl_trend.size(); ++i)
{
a.log.insert("log","趋势退出"+cl_trend[i]->name+" 编号"+to_string(i));
delete cl_trend[i];
}
cl_trend.clear();

cout<<"退出策略完成"<<endl;
}


//查询成交通知
if(str == "cj")
{
for(auto it=a.position_map.begin();it !=a.position_map.end();++it)
{
if(it->second.trade_list.size() > 0)
{
cout<<it->first<<"交易次数:"<<it->second.trade_list.size()<<endl;
}
for(auto it1=it->second.trade_list.begin();it1 != it->second.trade_list.end();++it1)
{
cout<<it1->InstrumentID<<",方向:"<<it1->Direction<<",价格:"<<it1->Price<<",数量:"<<it1->Volume<<",时间:"<<it1->TradeTime<<",交易日:"<<it1->TradingDay<<endl;
}
}
cout<<"完毕"<<endl;
}


///请求查询银期签约关系
if(str == "cxyqqygx")
{
CThostFtdcQryAccountregisterField pQryAccountregister={0};
cout<<"操作返回值:"<<a.trader_api_p->ReqQryAccountregister(&pQryAccountregister,a.nrequestid)<<endl;
cout<<"完毕"<<endl;
}


///期货发起银行资金转期货请求
if(str =="ytq")
{
CThostFtdcReqTransferField ReqTransfer={0};//定义结构体

///业务功能码
//	TThostFtdcTradeCodeType	TradeCode;
///银行代码
//	TThostFtdcBankIDType	BankID;
cout<<"请输入银行代码"<<endl;
cin>>ReqTransfer.BankID;
//		///银行分支机构代码
//		//	TThostFtdcBankBrchIDType	BankBranchID;
strncpy(ReqTransfer.BankBranchID,"0000", sizeof(ReqTransfer.BankBranchID));
//			///期商代码
//			//	TThostFtdcBrokerIDType	BrokerID;
strncpy(ReqTransfer.BrokerID,a.broker_str.c_str(), sizeof(ReqTransfer.BrokerID));
///期商分支机构代码
//				//	TThostFtdcFutureBranchIDType	BrokerBranchID;
strncpy(ReqTransfer.BrokerBranchID,"0000", sizeof(ReqTransfer.BrokerBranchID));
//					///交易日期
//					//	TThostFtdcTradeDateType	TradeDate;
//						///交易时间
//						//	TThostFtdcTradeTimeType	TradeTime;
//							///银行流水号
//							//	TThostFtdcBankSerialType	BankSerial;
//								///交易系统日期 
//								//	TThostFtdcTradeDateType	TradingDay;
//									///银期平台消息流水号
//									//	TThostFtdcSerialType	PlateSerial;
//										///最后分片标志
//										//	TThostFtdcLastFragmentType	LastFragment;
//											///会话号
//											//	TThostFtdcSessionIDType	SessionID;
//												///客户姓名
//												//	TThostFtdcIndividualNameType	CustomerName;
//													///证件类型
//													//	TThostFtdcIdCardTypeType	IdCardType;
ReqTransfer.IdCardType=1;
///证件号码
//														//	TThostFtdcIdentifiedCardNoType	IdentifiedCardNo;
cout<<"请输入证件号码"<<endl;
cin>>ReqTransfer.IdentifiedCardNo;
//															///客户类型
//															//	TThostFtdcCustTypeType	CustType;
///银行帐号
//																//	TThostFtdcBankAccountType	BankAccount;
cout<<"请输入银行账号"<<endl;
cin>>ReqTransfer.BankAccount;
//																	///银行密码
//																	//	TThostFtdcPasswordType	BankPassWord;
cout<<"请输入银行密码"<<endl;
cin>>ReqTransfer.BankPassWord;
//																		///投资者帐号
//																		//	TThostFtdcAccountIDType	AccountID;
strncpy(ReqTransfer.AccountID,a.user_str.c_str(), sizeof(ReqTransfer.AccountID));
//																			///期货密码
//	TThostFtdcPasswordType	Password;
cout<<"请输入期货密码"<<endl;
cin>>ReqTransfer.Password;
//																				///安装编号
//																				//	TThostFtdcInstallIDType	InstallID;
//																					///期货公司流水号
//																					//	TThostFtdcFutureSerialType	FutureSerial;
//																						///用户标识
//																						//	TThostFtdcUserIDType	UserID;
//																							///验证客户证件号码标志
//																							//	TThostFtdcYesNoIndicatorType	VerifyCertNoFlag;
//																								///币种代码
//																								//	TThostFtdcCurrencyIDType	CurrencyID;
strncpy(ReqTransfer.CurrencyID,"CNY", sizeof(ReqTransfer.CurrencyID));
///转帐金额
//	TThostFtdcTradeAmountType	TradeAmount;
cout<<"请输入转账金额"<<endl;
cin>>ReqTransfer.TradeAmount;
//																										///期货可取金额
//																										//	TThostFtdcTradeAmountType	FutureFetchAmount;
//																											///费用支付标志
//																											//	TThostFtdcFeePayFlagType	FeePayFlag;
//																												///应收客户费用
//																												//	TThostFtdcCustFeeType	CustFee;
//																													///应收期货公司费用
//																													//	TThostFtdcFutureFeeType	BrokerFee;
//																														///发送方给接收方的消息
//																														//	TThostFtdcAddInfoType	Message;
//																															///摘要
//																															//	TThostFtdcDigestType	Digest;
//																																///银行帐号类型
//																																//	TThostFtdcBankAccTypeType	BankAccType;
//																																	///渠道标志
//																																	//	TThostFtdcDeviceIDType	DeviceID;
//																																		///期货单位帐号类型
//																																		//	TThostFtdcBankAccTypeType	BankSecuAccType;
//																																			///期货公司银行编码
//																																			//	TThostFtdcBankCodingForFutureType	BrokerIDByBank;
//																																				///期货单位帐号
//																																				//	TThostFtdcBankAccountType	BankSecuAcc;
//																																					///银行密码标志
//																																					//	TThostFtdcPwdFlagType	BankPwdFlag;
//																																						///期货资金密码核对标志
//																																						//	TThostFtdcPwdFlagType	SecuPwdFlag;
//																																							///交易柜员
//																																							//	TThostFtdcOperNoType	OperNo;
//																																								///请求编号
//																																								//	TThostFtdcRequestIDType	RequestID;
//																																									///交易ID
//																																									//	TThostFtdcTIDType	TID;
//																																										///转账交易状态
//																																										//	TThostFtdcTransferStatusType	TransferStatus;
///长客户姓名
//	TThostFtdcLongIndividualNameType	LongCustomerName;


cout<<"操作返回值:"<<a.trader_api_p->ReqFromBankToFutureByFuture(&ReqTransfer, a.nrequestid++)<<endl;
cout<<"完毕"<<endl;
}


///期货发起期货资金转银行请求
if(str =="qty")
{
CThostFtdcReqTransferField ReqTransfer={0};

///业务功能码
//	TThostFtdcTradeCodeType	TradeCode;
///银行代码
//	TThostFtdcBankIDType	BankID;
cout<<"请输入银行代码"<<endl;
cin>>ReqTransfer.BankID;
//		///银行分支机构代码
//		//	TThostFtdcBankBrchIDType	BankBranchID;
strncpy(ReqTransfer.BankBranchID,"0000", sizeof(ReqTransfer.BankBranchID));
//			///期商代码
//			//	TThostFtdcBrokerIDType	BrokerID;
strncpy(ReqTransfer.BrokerID,a.broker_str.c_str(), sizeof(ReqTransfer.BrokerID));
///期商分支机构代码
//				//	TThostFtdcFutureBranchIDType	BrokerBranchID;
strncpy(ReqTransfer.BrokerBranchID,"0000", sizeof(ReqTransfer.BrokerBranchID));
//					///交易日期
//					//	TThostFtdcTradeDateType	TradeDate;
//						///交易时间
//						//	TThostFtdcTradeTimeType	TradeTime;
//							///银行流水号
//							//	TThostFtdcBankSerialType	BankSerial;
//								///交易系统日期 
//								//	TThostFtdcTradeDateType	TradingDay;
//									///银期平台消息流水号
//									//	TThostFtdcSerialType	PlateSerial;
//										///最后分片标志
//										//	TThostFtdcLastFragmentType	LastFragment;
//											///会话号
//											//	TThostFtdcSessionIDType	SessionID;
//												///客户姓名
//												//	TThostFtdcIndividualNameType	CustomerName;
//													///证件类型
//													//	TThostFtdcIdCardTypeType	IdCardType;
ReqTransfer.IdCardType=1;
///证件号码
//														//	TThostFtdcIdentifiedCardNoType	IdentifiedCardNo;
cout<<"请输入证件号码"<<endl;
cin>>ReqTransfer.IdentifiedCardNo;
//															///客户类型
//															//	TThostFtdcCustTypeType	CustType;
///银行帐号
//																//	TThostFtdcBankAccountType	BankAccount;
cout<<"请输入银行账号"<<endl;
cin>>ReqTransfer.BankAccount;
//																	///银行密码
//																	//	TThostFtdcPasswordType	BankPassWord;
cout<<"请输入银行密码"<<endl;
cin>>ReqTransfer.BankPassWord;
//																		///投资者帐号
//																		//	TThostFtdcAccountIDType	AccountID;
strncpy(ReqTransfer.AccountID,a.user_str.c_str(), sizeof(ReqTransfer.AccountID));
//																			///期货密码
//	TThostFtdcPasswordType	Password;
cout<<"请输入期货密码"<<endl;
cin>>ReqTransfer.Password;
//																				///安装编号
//																				//	TThostFtdcInstallIDType	InstallID;
//																					///期货公司流水号
//																					//	TThostFtdcFutureSerialType	FutureSerial;
//																						///用户标识
//																						//	TThostFtdcUserIDType	UserID;
//																							///验证客户证件号码标志
//																							//	TThostFtdcYesNoIndicatorType	VerifyCertNoFlag;
//																								///币种代码
//																								//	TThostFtdcCurrencyIDType	CurrencyID;
strncpy(ReqTransfer.CurrencyID,"CNY", sizeof(ReqTransfer.CurrencyID));
///转帐金额
//	TThostFtdcTradeAmountType	TradeAmount;
cout<<"请输入转账金额"<<endl;
cin>>ReqTransfer.TradeAmount;
//																										///期货可取金额
//																										//	TThostFtdcTradeAmountType	FutureFetchAmount;
//																											///费用支付标志
//																											//	TThostFtdcFeePayFlagType	FeePayFlag;
//																												///应收客户费用
//																												//	TThostFtdcCustFeeType	CustFee;
//																													///应收期货公司费用
//																													//	TThostFtdcFutureFeeType	BrokerFee;
//																														///发送方给接收方的消息
//																														//	TThostFtdcAddInfoType	Message;
//																															///摘要
//																															//	TThostFtdcDigestType	Digest;
//																																///银行帐号类型
//																																//	TThostFtdcBankAccTypeType	BankAccType;
//																																	///渠道标志
//																																	//	TThostFtdcDeviceIDType	DeviceID;
//																																		///期货单位帐号类型
//																																		//	TThostFtdcBankAccTypeType	BankSecuAccType;
//																																			///期货公司银行编码
//																																			//	TThostFtdcBankCodingForFutureType	BrokerIDByBank;
//																																				///期货单位帐号
//																																				//	TThostFtdcBankAccountType	BankSecuAcc;
//																																					///银行密码标志
//																																					//	TThostFtdcPwdFlagType	BankPwdFlag;
//																																						///期货资金密码核对标志
//																																						//	TThostFtdcPwdFlagType	SecuPwdFlag;
//																																							///交易柜员
//																																							//	TThostFtdcOperNoType	OperNo;
//																																								///请求编号
//																																								//	TThostFtdcRequestIDType	RequestID;
//																																									///交易ID
//																																									//	TThostFtdcTIDType	TID;
//																																										///转账交易状态
//																																										//	TThostFtdcTransferStatusType	TransferStatus;
//																																											///长客户姓名
//																																											//	TThostFtdcLongIndividualNameType	LongCustomerName;

cout<<"操作返回:"<<a.trader_api_p->ReqFromFutureToBankByFuture(&ReqTransfer, a.nrequestid++)<<endl;
cout<<"完毕"<<endl;
}


///期货发起查询银行余额请求
if(str=="cxyhye")
{
CThostFtdcReqQueryAccountField ReqQueryAccount={0};

//业务功能码
////ReqQueryAccount.TradeCode;
////银行代码
////ReqQueryAccount.BankID;
cout<<"请输入银行代码"<<endl;
cin>>ReqQueryAccount.BankID;

////银行分支机构代码
////ReqQueryAccount.BankBranchID;
strncpy(ReqQueryAccount.BankBranchID,"0000", sizeof(ReqQueryAccount.BankBranchID));
////期商代码
////ReqQueryAccount.BrokerID;
strncpy(ReqQueryAccount.BrokerID, a.broker_str.c_str(), sizeof(ReqQueryAccount.BrokerID));
////期商分支机构代码
////ReqQueryAccount.BrokerBranchID;
strncpy(ReqQueryAccount.BrokerBranchID,"0000", sizeof(ReqQueryAccount.BrokerBranchID));
////交易日期
////ReqQueryAccount.TradeDate;
////交易时间
////ReqQueryAccount.TradeTime;
////银行流水号
////ReqQueryAccount.BankSerial;
////交易系统日期 
////ReqQueryAccount.TradingDay;
////银期平台消息流水号
////ReqQueryAccount.PlateSerial;
////最后分片标志
////ReqQueryAccount.LastFragment;
////会话号
////ReqQueryAccount.SessionID;
////客户姓名
////ReqQueryAccount.CustomerName;
////证件类型
////ReqQueryAccount.IdCardType;
ReqQueryAccount.IdCardType=1;
////证件号码
////ReqQueryAccount.IdentifiedCardNo;
cout<<"请输入证件号码"<<endl;
cin>>ReqQueryAccount.IdentifiedCardNo;
////客户类型
////ReqQueryAccount.CustType;
////银行帐号
////ReqQueryAccount.BankAccount;
cout<<"请输入银行账号"<<endl;
cin>>ReqQueryAccount.BankAccount;
////银行密码
////ReqQueryAccount.BankPassWord;
cout<<"请输入银行密码"<<endl;
cin>>ReqQueryAccount.BankPassWord;
////投资者帐号
////ReqQueryAccount.AccountID;
strncpy(ReqQueryAccount.AccountID,a.user_str.c_str(), sizeof(ReqQueryAccount.AccountID));
////期货密码
////ReqQueryAccount.Password;
cout<<"请输入期货密码"<<endl;
cin>>ReqQueryAccount.Password;
////期货公司流水号
////ReqQueryAccount.FutureSerial;
////安装编号
////ReqQueryAccount.InstallID;
////用户标识
////ReqQueryAccount.UserID;
////验证客户证件号码标志
////ReqQueryAccount.VerifyCertNoFlag;
////币种代码
////ReqQueryAccount.CurrencyID;
strncpy(ReqQueryAccount.CurrencyID,"CNY", sizeof(ReqQueryAccount.CurrencyID));
////摘要
////ReqQueryAccount.Digest;
////银行帐号类型
////ReqQueryAccount.BankAccType;
////渠道标志
////ReqQueryAccount.DeviceID;
////期货单位帐号类型
////ReqQueryAccount.BankSecuAccType;
////期货公司银行编码
////ReqQueryAccount.BrokerIDByBank;
////期货单位帐号
////ReqQueryAccount.BankSecuAcc;
////银行密码标志
////ReqQueryAccount.BankPwdFlag;
////期货资金密码核对标志
////ReqQueryAccount.SecuPwdFlag;
ReqQueryAccount.SecuPwdFlag=THOST_FTDC_BPWDF_BlankCheck;

////交易柜员
////ReqQueryAccount.OperNo;
////请求编号
////ReqQueryAccount.RequestID;
////交易ID
////ReqQueryAccount.TID;
////长客户姓名
////ReqQueryAccount.LongCustomerName;

cout<<"操作返回:"<<a.trader_api_p->ReqQueryBankAccountMoneyByFuture(&ReqQueryAccount,a.nrequestid++)<<endl;
cout<<"完毕"<<endl;
}


//排序
if(str == "px")
{

vector<CThostFtdcDepthMarketDataField> data_list;

a.up_hot();
for(auto it=a.hot_map_vector.begin();it != a.hot_map_vector.end();++it)
{
if(it->second.size() > 0 && a.hy_map[it->second.begin()->InstrumentID].ProductClass=='1')
{
data_list.push_back(*it->second.begin() );
}
}
//排序方式
cout<<"请输入排序方式, 1 涨幅; 2 持仓量; 3 成交量; 4 成交金额;5 增仓量;6 增仓比例."<<endl;

int fs=0;
cin>>fs;

switch(fs)
{
case 1 :
sort(data_list.begin(),data_list.end(),[](CThostFtdcDepthMarketDataField &a,CThostFtdcDepthMarketDataField &b){return a.LastPrice / a.PreClosePrice > b.LastPrice / b.PreClosePrice;});
break;
case 2 :
sort(data_list.begin(),data_list.end(),[](CThostFtdcDepthMarketDataField &a,CThostFtdcDepthMarketDataField &b){return a.OpenInterest > b.OpenInterest;});
break;
case 3 :
sort(data_list.begin(),data_list.end(),[](CThostFtdcDepthMarketDataField &a,CThostFtdcDepthMarketDataField &b){return a.Volume > b.Volume;});
break;
case 4 :
sort(data_list.begin(),data_list.end(),[](CThostFtdcDepthMarketDataField &a,CThostFtdcDepthMarketDataField &b){return a.Turnover > b.Turnover;});
break;
case 5 :
sort(data_list.begin(),data_list.end(),[](CThostFtdcDepthMarketDataField &a,CThostFtdcDepthMarketDataField &b){return (a.OpenInterest - a.PreOpenInterest) > (b.OpenInterest - b.PreOpenInterest);});
break;
case 6 :
sort(data_list.begin(),data_list.end(),[](CThostFtdcDepthMarketDataField &a,CThostFtdcDepthMarketDataField &b){return (a.OpenInterest / a.PreOpenInterest) > (b.OpenInterest / b.PreOpenInterest);});
break;
}
for(auto it=data_list.begin();it !=data_list.end();++it)
{
printf("%.2f",it->LastPrice / it->PreClosePrice * 100 - 100);
cout<<","<<a.zm.zm(a.hy_map[it->InstrumentID].InstrumentName)<<",价格"<<it->LastPrice<<",代码:"<< it->InstrumentID<<",持仓"<<it->OpenInterest<<",增减:"<<it->OpenInterest - it->PreOpenInterest<<",成交量"<<it->Volume<<",金额"<<it->Turnover<<",换手:"<<it->Volume / it->OpenInterest * 100<<"%"<<endl;
}
double turnover = accumulate(data_list.begin(), data_list.end(), 0.0, [](double x, CThostFtdcDepthMarketDataField &y){return x+=y.Turnover;});
cout<<"总数量:"<<data_list.size()<<",总金额:"<<turnover<<endl;
data_list.clear();
cout<<"完毕"<<endl;
}


///请求查询期权合约手续费
if(str == "cxqqsxf")
{
CThostFtdcQryOptionInstrCommRateField QryOptionInstrCommRate={0};

///经纪公司代码
//TThostFtdcBrokerIDType	BrokerID;
strncpy(QryOptionInstrCommRate.BrokerID,a.broker_str.c_str(), sizeof(QryOptionInstrCommRate.BrokerID));
//		///投资者代码
//			TThostFtdcInvestorIDType	InvestorID;
strncpy(QryOptionInstrCommRate.InvestorID,a.user_str.c_str(), sizeof(QryOptionInstrCommRate.InvestorID));
//				///保留的无效字段
//					TThostFtdcOldInstrumentIDType	reserve1;
//						///交易所代码
//	TThostFtdcExchangeIDType	ExchangeID;
//								///投资单元代码
//									TThostFtdcInvestUnitIDType	InvestUnitID;
//										///合约代码
//											TThostFtdcInstrumentIDType	InstrumentID;
cout<<"请输入合约代码"<<endl;
cin>>QryOptionInstrCommRate.InstrumentID;

cout<<"操作返回值:"<<a.trader_api_p->ReqQryOptionInstrCommRate(&QryOptionInstrCommRate, a.nrequestid++)<<endl;
cout<<"完毕"<<endl;
}


//请求查询合约手续费率
if(str == "cxsxf")
{
CThostFtdcQryInstrumentCommissionRateField QryInstrumentCommissionRate;
memset(&QryInstrumentCommissionRate,0,sizeof(QryInstrumentCommissionRate));
strncpy(QryInstrumentCommissionRate.BrokerID,a.broker_str.c_str(), sizeof(QryInstrumentCommissionRate.BrokerID));
strncpy(QryInstrumentCommissionRate.InvestorID,a.user_str.c_str(), sizeof(QryInstrumentCommissionRate.InvestorID));

cout<<"请输入合约代码"<<endl;

cin>>QryInstrumentCommissionRate.InstrumentID;
cout<<a.trader_api_p->ReqQryInstrumentCommissionRate(&QryInstrumentCommissionRate,a.nrequestid++)<<endl;
cout<<"完毕"<<endl;
}


///查询期权交易成本
if(str == "cxqqjycb")
{
CThostFtdcQryOptionInstrTradeCostField  data = {0};
cout<<"期权代码"<<endl;									///合约代码
cin>>data.InstrumentID;
///经纪公司代码
strcpy(data.BrokerID, a.broker_str.c_str());
///投资者代码
strcpy(data.InvestorID, a.user_str.c_str());
//投机套保标志
data.HedgeFlag = THOST_FTDC_BHF_Speculation;
///期权合约报价
data.InputPrice = a.tick_map[data.InstrumentID].LastPrice;
///标的价格,填0则用昨结算价
data.UnderlyingPrice = 0;
///交易所代码
strcpy(data.ExchangeID, a.hy_map[data.InstrumentID].ExchangeID);
///投资单元代码
//			TThostFtdcInvestUnitIDType	InvestUnitID;

cout<<"返回:"<<a.trader_api_p->ReqQryOptionInstrTradeCost(&data, a.nrequestid++)<<endl;
cout<<"完毕"<<endl;
}


///请求查询分类合约
if(str == "cxflhy")
{
CThostFtdcQryClassifiedInstrumentField data = { 0 };
///合约代码
///	TThostFtdcInstrumentIDType	InstrumentID;
///		///交易所代码
///			TThostFtdcExchangeIDType	ExchangeID;
///				///合约在交易所的代码
///					TThostFtdcExchangeInstIDType	ExchangeInstID;
///						///产品代码
///							TThostFtdcInstrumentIDType	ProductID;
///								///合约交易状态
data.TradingType = '0';
///所有状态
///#define THOST_FTDC_TD_ALL '0'
///交易
///#define THOST_FTDC_TD_TRADE '1'
//非交易
///#define THOST_FTDC_TD_UNTRADE '2'


///										///合约分类类型
cout<<"分类类型"<<endl;
cin>>data.ClassType;// = '0';
///所有合约
///#define THOST_FTDC_INS_ALL '0'
//////期货、即期、期转现、Tas、金属指数合约
///#define THOST_FTDC_INS_FUTURE '1'
//////期货、现货期权合约
///#define THOST_FTDC_INS_OPTION '2'
//////组合合约
///#define THOST_FTDC_INS_COMB '3'


cout<<"返回值:"<<a.trader_api_p->ReqQryClassifiedInstrument(&data, a.nrequestid++)<<endl;
cout<<"完毕"<<endl;
}


//请求查询合约保证金率
if(str == "cxbzj")
{
CThostFtdcQryInstrumentMarginRateField QryInstrumentMarginRate;
memset(&QryInstrumentMarginRate,0,sizeof(QryInstrumentMarginRate));
strncpy(QryInstrumentMarginRate.BrokerID,a.broker_str.c_str(), sizeof(QryInstrumentMarginRate.BrokerID));
strncpy(QryInstrumentMarginRate.InvestorID,a.user_str.c_str(), sizeof(QryInstrumentMarginRate.InvestorID));

cout<<a.trader_api_p->ReqQryInstrumentMarginRate(&QryInstrumentMarginRate,a.nrequestid++)<<endl;
cout<<"完毕"<<endl;
}


//请求查询合约保证金率特定合约
if(str == "b")
{
CThostFtdcQryInstrumentMarginRateField QryInstrumentMarginRate;
memset(&QryInstrumentMarginRate,0,sizeof(QryInstrumentMarginRate));
strncpy(QryInstrumentMarginRate.BrokerID,a.broker_str.c_str(), sizeof(QryInstrumentMarginRate.BrokerID));
strncpy(QryInstrumentMarginRate.InvestorID,a.user_str.c_str(), sizeof(QryInstrumentMarginRate.InvestorID));
QryInstrumentMarginRate.HedgeFlag='1';
cout<<"请输入合约代码"<<endl;
cin>>QryInstrumentMarginRate.InstrumentID;
cout<<a.trader_api_p->ReqQryInstrumentMarginRate(&QryInstrumentMarginRate,a.nrequestid++)<<endl;
cout<<"完毕"<<endl;
}


//申请组合录入请求
if(str == "sqzhlr")
{
CThostFtdcInputCombActionField data = { 0 };
//经纪公司代码
strcpy(data.BrokerID, a.broker_str.c_str() );
//投资者代码
strcpy(data.InvestorID, a.user_str.c_str() );
//保留的无效字段
//TThostFtdcOldInstrumentIDType	reserve1;
///组合引用
sprintf(data.CombActionRef, "%12d", a.nrequestid++);
///用户代码
strcpy(data.UserID, a.user_str.c_str() );
///买卖方向
cout<<"方向, 买入:'0', 卖出:'1'"<<endl;
cin>>data.Direction;
//数量
cout<<"数量"<<endl;
cin>>data.Volume;
///组合指令方向
cout<<"组合指令方向,组合:'0', 拆分:'1', 删除组合单:'2'"<<endl;
cin>>data.CombDirection;
///投机套保标志
data.HedgeFlag = '1';
///交易所代码
cout<<"交易所代码"<<endl;
cin>>data.ExchangeID;
///保留的无效字段
//TThostFtdcOldIPAddressType	reserve2;
///Mac地址
//TThostFtdcMacAddressType	MacAddress;
///投资单元代码
//TThostFtdcInvestUnitIDType	InvestUnitID;
///前置编号
data.FrontID = a.frontid;
//会话编号
data.SessionID = a.sessionid;
//合约代码
cout<<"合约代码"<<endl;
string dm;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
getline(cin, dm, '\n');
strcpy(data.InstrumentID, dm.c_str());
//IP地址
//TThostFtdcIPAddressType	IPAddress;

cout<<a.trader_api_p->ReqCombActionInsert(&data, a.nrequestid++)<<endl;
cout<<"完毕"<<endl;
}


//获取主力合约
if(str == "hot")
{
ofstream wj(a.path_str+"/log/hot.txt",ios_base::app);
a.up_hot();
int num=0;
for(auto it=a.hot_map_vector.begin();it!=a.hot_map_vector.end();++it)
{
//if(it->second.begin() !=it->second.end())
if(it->second.begin() != it->second.end() && a.hy_map[it->second.begin()->InstrumentID].ProductClass=='1' && it->second.begin()->OpenInterest > 0)
{
//cout<<it->first<<"合约";
//cout << it->second.begin()->InstrumentID<<endl;
cout<<it->second.begin()->InstrumentID<<"价格:"<< it->second.begin()->LastPrice<<",成交量:"<< it->second.begin()->Volume<<",持仓量:"<< it->second.begin()->OpenInterest<<endl;
wj << it->second.begin()->InstrumentID<<endl;
num++;
}
}
wj.close();
cout<<"总计:"<<num<<endl;
cout<<"完毕"<<endl;
}


//获取主力期权
if(str == "hot_q")
{
a.up_hot();
int num=0;
cout<<"主力期权"<<endl;
for(auto it=a.hot_map_vector.begin(); it != a.hot_map_vector.end(); ++it)
{
if(it->second.begin() != it->second.end() && a.hy_map[it->second.begin()->InstrumentID].ProductClass=='2' && it->second.begin()->OpenInterest > 0)
{
cout<<it->second.begin()->InstrumentID<<"价格:"<< it->second.begin()->LastPrice<<",成交量:"<< it->second.begin()->Volume<<",持仓量:"<< it->second.begin()->OpenInterest<<endl;
num++;
}
}
cout<<"总计:"<<num<<endl;
cout<<"完毕"<<endl;
}


//更新主力
if(str=="hot_update")
{
a.up_hot();
cout<<"完毕"<<endl;
}


//获取主力列表
if(str == "hot_list")
{
cout<<"请输入合约类型";
string name;
cin>> name;
if(a.hot_map_vector.find(name) != a.hot_map_vector.end())
{
for(auto it=a.hot_map_vector[name].begin();it !=a.hot_map_vector[name].end();++it)
{
cout<<it->InstrumentID<<endl;
}
cout<<"合约个数"<<a.hot_map_vector[name].size() << endl;
}
cout<<"完毕"<<endl;
}


//组合查询
if(str == "zh")
{
string name;
//TThostFtdcInstrumentIDType name = "";
struct a_b
{
string a,b;
double c,d;
};
map<int,a_b> a_b_map;
cout<<"请输入合约代码"<<endl;
cin>> name;
for (auto it = a.hy_map.begin();it != a.hy_map.end();++it)
{
if(it->second.ProductClass == '2' and it->second.UnderlyingInstrID == name and it->second.OptionsType == '1')
{
a_b_map[it->second.StrikePrice].a = it->second.InstrumentID;
}
if(it->second.ProductClass == '2' and it->second.UnderlyingInstrID == name and it->second.OptionsType == '2')
{
a_b_map[it->second.StrikePrice].b = it->second.InstrumentID;
}
}

cout<<a.tick_map[name].InstrumentID <<"当前价"<<a.tick_map[name].LastPrice<<endl;
for(auto it=a_b_map.begin();it != a_b_map.end();++it)
{
//cout<<"行权价"<<it->first<<"认购"<<a.tick_map[it->second.a].LastPrice<<"认沽"<<a.tick_map[it->second.b].LastPrice<<endl;
//cout<<"行权价"<<it->first<<"买价值"<<a.tick_map[it->second.a].BidPrice1 - a.tick_map[it->second.b].AskPrice1 + it->first - a.tick_map[name].AskPrice1<<"卖价值"<<a.tick_map[name].BidPrice1 - it->first - a.tick_map[it->second.a].AskPrice1 + a.tick_map[it->second.b].BidPrice1<<endl;
cout<<"行权价"<<it->first;
if(a.tick_map[it->second.a].BidVolume1 > 0 and a.tick_map[it->second.b].AskVolume1 > 0 and a.tick_map[name].AskVolume1 > 0)
{
it->second.c = a.tick_map[it->second.a].BidPrice1 - a.tick_map[it->second.b].AskPrice1 + it->first - a.tick_map[name].AskPrice1;
cout<<"买"<<it->second.c;
}
if(a.tick_map[name].BidVolume1 > 0 and a.tick_map[it->second.a].AskVolume1 > 0 and a.tick_map[it->second.b].BidVolume1 > 0)
{
it->second.d = a.tick_map[name].BidPrice1 - it->first - a.tick_map[it->second.a].AskPrice1 + a.tick_map[it->second.b].BidPrice1;
cout<<"卖"<<it->second.d;
}
cout << endl;

}

vector<a_b> v_a_b;
for(auto it=a_b_map.begin();it!=a_b_map.end();++it)
{
v_a_b.push_back(it->second);
}
if(v_a_b.size() > 0)
{
sort(v_a_b.begin(),v_a_b.end(),[](a_b &a,a_b &b){return max(a.c,a.d) > max(b.c,b.d);});
cout<<v_a_b.begin()->a<<"价值"<<v_a_b.begin()->c<<v_a_b.begin()->b<<"价值"<<v_a_b.begin()->d<<endl;
cout<<"是否报单"<<endl;
string  yn="";
cin>>yn;
TThostFtdcInstrumentIDType hy="";
if(yn == "buy")
{
strncpy(hy,name.c_str(), sizeof(hy));
a.bd_auto(hy,'0',a.tick_map[name].AskPrice1,1,cccc);
strncpy(hy,v_a_b.begin()->a.c_str(), sizeof(hy));
a.bd_auto(hy,'1',a.tick_map[v_a_b.begin()->a].BidPrice1,1,cccc);
strncpy(hy,v_a_b.begin()->b.c_str(), sizeof(hy));
a.bd_auto(hy,'0',a.tick_map[v_a_b.begin()->b].AskPrice1,1,cccc);
}
if(yn == "sell")
{
strncpy(hy,name.c_str(), sizeof(hy));
a.bd_auto(hy,'1',a.tick_map[name].BidPrice1,1,cccc);
strncpy(hy,v_a_b.begin()->a.c_str(), sizeof(hy));
a.bd_auto(hy,'0',a.tick_map[v_a_b.begin()->a].AskPrice1,1,cccc);
strncpy(hy,v_a_b.begin()->b.c_str(), sizeof(hy));
a.bd_auto(hy,'1',a.tick_map[v_a_b.begin()->b].BidPrice1,1,cccc);
}

}
cout<<"总计"<<a_b_map.size()<<"个行权价"<<endl;
cout<<"完毕"<<endl;
}


//期权自对冲
if(str == "zdc")
{

string name;
cout<<"请输入合约"<<endl;
cin>>name;
CThostFtdcInputOptionSelfCloseField jg = { 0 };
strncpy(jg.BrokerID, a.broker_str.c_str(), sizeof(jg.BrokerID));
strncpy(jg.InvestorID,a.user_str.c_str(), sizeof(jg.InvestorID));
strncpy(jg.InstrumentID, name.c_str(), sizeof(jg.InstrumentID));
strncpy(jg.UserID, a.user_str.c_str(), sizeof(jg.UserID));
jg.Volume = 1;
jg.HedgeFlag = THOST_FTDC_HF_Speculation;
jg.OptSelfCloseFlag = THOST_FTDC_OSCF_CloseSelfOptionPosition;
strncpy(jg.ExchangeID, a.hy_map[name].ExchangeID, sizeof(jg.ExchangeID));

strncpy(jg.AccountID, a.user_str.c_str(), sizeof(jg.AccountID));
strncpy(jg.CurrencyID, "CNY", sizeof(jg.CurrencyID));
cout<<a.trader_api_p->ReqOptionSelfCloseInsert(&jg, a.nrequestid++)<<endl;
}


//执行宣告录入请求
if(str == "xg")
{
CThostFtdcInputExecOrderField pInputExecOrder;
memset(&pInputExecOrder,0,sizeof(CThostFtdcInputExecOrderField));


///经纪公司代码
strncpy(pInputExecOrder.BrokerID, a.broker_str.c_str(), a.broker_str.size());
///投资者代码
strncpy(pInputExecOrder.InvestorID, a.user_str.c_str(), a.user_str.size() );
///执行宣告引用
//	TThostFtdcOrderRefType	ExecOrderRef;
string orderref=to_string(++a.orderref);
strncpy(pInputExecOrder.ExecOrderRef, orderref.c_str(), orderref.size() ) ;
///用户代码
//	TThostFtdcUserIDType	UserID;
strncpy(pInputExecOrder.UserID, a.user_str.c_str(), a.user_str.size() );
cout<<"数量"<<endl;
cin>>pInputExecOrder.Volume;// = 1;
///请求编号
//	TThostFtdcRequestIDType	RequestID;
//pInputExecOrder.RequestID = a.nrequestid++;
///业务单元
//	TThostFtdcBusinessUnitType	BusinessUnit;
//cout<<"开平标志"<<endl;
pInputExecOrder.OffsetFlag ='1';
///投机套保标志
pInputExecOrder.HedgeFlag='1';
cout<<"执行类型"<<endl;
cin>>pInputExecOrder.ActionType;//='1';
cout<<"保留头寸申请的持仓方向"<<endl;
cin>>pInputExecOrder.PosiDirection;//='1';
pInputExecOrder.ReservePositionFlag = THOST_FTDC_EOPF_Reserve;//期权行权后是否保留期货头寸的标记类型
cout<<"期权行权后生成的头寸是否自动平仓"<<endl;
cin>>pInputExecOrder.CloseFlag;//='0';
///交易所代码
//	TThostFtdcExchangeIDType	ExchangeID;
///投资单元代码
//	TThostFtdcInvestUnitIDType	InvestUnitID;
///资金账号
//	TThostFtdcAccountIDType	AccountID;
///币种代码
//	TThostFtdcCurrencyIDType	CurrencyID;
///交易编码
//	TThostFtdcClientIDType	ClientID;
///Mac地址
//	TThostFtdcMacAddressType	MacAddress;
///合约代码
cout<<"请输入合约"<<endl;
cin>>pInputExecOrder.InstrumentID;

//交易所id
strncpy(pInputExecOrder.ExchangeID, a.hy_map[pInputExecOrder.InstrumentID].ExchangeID, sizeof(pInputExecOrder.ExchangeID));

///IP地址
//	TThostFtdcIPAddressType	IPAddress;

cout<<a.trader_api_p->ReqExecOrderInsert(&pInputExecOrder, a.nrequestid++)<<endl;
cout<<"完毕"<<endl;
}


//行权
if(str == "xq")
{
CThostFtdcInputExecOrderField OrderInsert = { 0 };
strncpy(OrderInsert.BrokerID, a.broker_str.c_str(), a.broker_str.size());
strncpy(OrderInsert.InvestorID, a.user_str.c_str(), a.user_str.size());
cout<<"合约代码"<<endl;
cin>>OrderInsert.InstrumentID;//, "cu2410C76000");
strncpy(OrderInsert.ExchangeID, a.hy_map[OrderInsert.InstrumentID].ExchangeID, sizeof(OrderInsert.ExchangeID));
snprintf(OrderInsert.ExecOrderRef, sizeof(OrderInsert.ExecOrderRef), "%012d", a.orderref++);
strncpy(OrderInsert.UserID, a.user_str.c_str(), a.user_str.size());
cout<<"数量"<<endl;
cin>>OrderInsert.Volume;// = 1;
OrderInsert.RequestID = a.nrequestid++; 
OrderInsert.OffsetFlag = THOST_FTDC_OF_Close;//开平标志
OrderInsert.HedgeFlag = THOST_FTDC_HF_Speculation;//投机套保标志
cout<<"执行类型"<<endl;
cin>>OrderInsert.ActionType;// = THOST_FTDC_ACTP_Exec;//执行类型类型
OrderInsert.PosiDirection = THOST_FTDC_PD_Long;//持仓多空方向类型
OrderInsert.ReservePositionFlag = THOST_FTDC_EOPF_Reserve;//期权行权后是否保留期货头寸的标记类型
cout<<"期权行权后生成的头寸是否自动平仓"<<endl;
cin>>OrderInsert.CloseFlag;// = THOST_FTDC_EOCF_NotToClose;//期权行权后生成的头寸是否自动平仓类型
cout<<a.trader_api_p->ReqExecOrderInsert(&OrderInsert, a.nrequestid++)<<endl;;
cout<<"完毕"<<endl;
}


//合约状态
if(str == "zt")
{
string name;
cout<<"请输入合约"<<endl;
cin>>name;
if(a.tick_status_map.find(name) != a.tick_status_map.end())
{
cout<<a.tick_status_map[name]<<endl;
}
if(a.instrument_status_map.find(name) != a.instrument_status_map.end() )
{
auto status = a.instrument_status_map[name];
cout<<"交易所代码"<<status.ExchangeID<<"合约在交易所的代码"<<status.ExchangeInstID<<"结算组代码"<<status.SettlementGroupID<<"合约代码"<<status.InstrumentID<<endl;
cout<<"合约交易状态"<<status.InstrumentStatus<<"交易阶段编号"<<status.TradingSegmentSN<<"进入本状态时间"<<status.EnterTime<<"进入本状态原因"<<status.EnterReason<<endl;
}
cout<<"完毕"<<endl;
}


//读取行情
if(str == "read")
{

data.datas.clear();

cout<<"当前数量"<<data.datas.size()<<"请输入地址"<<endl;
string name;
cin >> name;
data.read(name,120000);
cout << "当前数量" << data.datas.size() << endl;
}


//写出bar
if(str == "write")
{
string name;
cout<<"请输入目标地址"<<endl;
cin>>name;

cout<<"写出"<<data.write(name, 1000000)<<endl;
cout<<"完毕"<<endl;
}


//读取文本行情
if(str == "read_txt")
{

data.datas.clear();

cout<<"当前数量"<<data.datas.size()<<endl;
string name;

cout<<"请输入文本地址"<<endl;
cin >> name;
cout<<"读入"<< data.read_txt(name, 1000000)<<endl;
cout<<"完毕"<<endl;
}


//写出行情到文本
if (str == "write_txt")
{
string name;
cout << "请输入目标地址" << endl;
cin >> name;
cout<<"写出"<< data.write_txt(name, 1000000)<<endl;
cout<<"完毕"<<endl;
}


//ticks到bars
if(str == "ticks_to_bars")
{
string name;
cout<<"请输入tick地址"<<endl;
cin>>name;
class_tick datas_tick;
cout<<"读入"<<datas_tick.read(name,1000000)<<endl;
CThostFtdcDepthMarketDataField tick = {0};
for(auto it=datas_tick.datas.begin();it != datas_tick.datas.end();++it)
{
tick.LastPrice=it->LastPrice;
strncpy(tick.TradingDay, it->TradingDay, sizeof(tick.TradingDay));
strncpy(tick.UpdateTime, it->UpdateTime, sizeof(tick.UpdateTime));
data.insert(tick);
}
cout<<"完毕"<<endl;
}


//合成
if(str == "hb")
{
class_tick datas_tick;

vector<string> ml_str,name_str;

ml_str.push_back("/mnt/g/ticks/20240427/");
ml_str.push_back("/mnt/g/ticks/20240504/");
ml_str.push_back("/mnt/g/ticks/20240511/");
ml_str.push_back("/mnt/g/ticks/20240518/");
ml_str.push_back("/mnt/g/ticks/20240525/");
ml_str.push_back("/mnt/g/ticks/20240601/");
ml_str.push_back("/mnt/g/ticks/20240608/");
ml_str.push_back("/mnt/g/ticks/20240615/");
ml_str.push_back("/mnt/g/ticks/20240622/");
ml_str.push_back("/mnt/g/ticks/20240629/");
ml_str.push_back("/mnt/g/ticks/20240706/");
ml_str.push_back("/mnt/g/ticks/20240713/");
ml_str.push_back("/mnt/g/ticks/20240720/");
ml_str.push_back("/mnt/g/ticks/20240727/");
ml_str.push_back("/mnt/g/ticks/20240803/");
ml_str.push_back("/mnt/g/ticks/20240810/");
ml_str.push_back("/mnt/g/ticks/20240817/");
ml_str.push_back("/mnt/g/ticks/20240824/");
ml_str.push_back("/mnt/g/ticks/20240831/");
ml_str.push_back("/mnt/g/ticks/20240907/");
ml_str.push_back("/mnt/g/ticks/20240914/");
ml_str.push_back("/mnt/g/ticks/20240921/");
ml_str.push_back("/mnt/g/ticks/20240928/");
ml_str.push_back("/mnt/g/ticks/20241005/");
ml_str.push_back("/mnt/g/ticks/20241012/");
ml_str.push_back("/mnt/g/ticks/20241019/");
ml_str.push_back("/mnt/g/ticks/20241026/");
ml_str.push_back("/mnt/g/ticks/20241102/");
ml_str.push_back("/mnt/g/ticks/20241109/");
ml_str.push_back("/mnt/g/ticks/20241116/");
ml_str.push_back("/mnt/g/ticks/20241123/");
ml_str.push_back("/mnt/g/ticks/20241130/");
ml_str.push_back("/mnt/g/ticks/20241207/");


name_str.push_back("AP501.dat");
name_str.push_back("AP505.dat");
name_str.push_back("CF501.dat");
name_str.push_back("CF505.dat");
name_str.push_back("FG501.dat");
name_str.push_back("FG505.dat");
name_str.push_back("MA501.dat");
name_str.push_back("MA505.dat");
name_str.push_back("OI501.dat");
name_str.push_back("OI505.dat");
name_str.push_back("RM501.dat");
name_str.push_back("RM505.dat");
name_str.push_back("SA501.dat");
name_str.push_back("SA505.dat");
name_str.push_back("SM501.dat");
name_str.push_back("SR501.dat");
name_str.push_back("SR505.dat");
name_str.push_back("TA501.dat");
name_str.push_back("TA505.dat");
name_str.push_back("UR501.dat");
name_str.push_back("UR505.dat");
name_str.push_back("a2501.dat");
name_str.push_back("a2505.dat");
name_str.push_back("b2501.dat");
name_str.push_back("b2505.dat");
name_str.push_back("ag2412.dat");
name_str.push_back("ag2502.dat");
name_str.push_back("ag2506.dat");
name_str.push_back("al2501.dat");
name_str.push_back("al2502.dat");
name_str.push_back("al2503.dat");
name_str.push_back("bu2412.dat");
name_str.push_back("bu2502.dat");
name_str.push_back("bu2506.dat");
name_str.push_back("c2501.dat");
name_str.push_back("c2505.dat");
name_str.push_back("fu2501.dat");
name_str.push_back("fu2505.dat");
name_str.push_back("ec2412.dat");
name_str.push_back("ec2502.dat");
name_str.push_back("ec2504.dat");
name_str.push_back("hc2501.dat");
name_str.push_back("hc2505.dat");
name_str.push_back("i2501.dat");
name_str.push_back("i2505.dat");
name_str.push_back("j2501.dat");
name_str.push_back("j2505.dat");
name_str.push_back("jd2501.dat");
name_str.push_back("jd2505.dat");
name_str.push_back("jd2509.dat");
name_str.push_back("jm2501.dat");
name_str.push_back("jm2505.dat");
name_str.push_back("l2501.dat");
name_str.push_back("l2505.dat");
name_str.push_back("l2509.dat");
name_str.push_back("m2501.dat");
name_str.push_back("m2505.dat");
name_str.push_back("m2509.dat");
name_str.push_back("p2501.dat");
name_str.push_back("p2505.dat");
name_str.push_back("rb2501.dat");
name_str.push_back("rb2505.dat");
name_str.push_back("rb2510.dat");
name_str.push_back("ru2501.dat");
name_str.push_back("ru2505.dat");
name_str.push_back("ru2509.dat");
name_str.push_back("sc2501.dat");
name_str.push_back("sc2502.dat");
name_str.push_back("sc2503.dat");
name_str.push_back("v2501.dat");
name_str.push_back("v2505.dat");
name_str.push_back("v2509.dat");
name_str.push_back("y2501.dat");
name_str.push_back("y2505.dat");
name_str.push_back("y2509.dat");



for(auto n_p=name_str.begin();n_p != name_str.end();++n_p)
{

data.datas.clear();

for(auto m_p=ml_str.begin();m_p!=ml_str.end();++m_p)
{

cout<<"读入"<<datas_tick.read((*m_p)+(*n_p),1000000)<<endl;
CThostFtdcDepthMarketDataField tick;
for(auto it=datas_tick.datas.begin();it != datas_tick.datas.end();++it)
{
tick.LastPrice=it->LastPrice;
strncpy(tick.TradingDay,it->TradingDay, sizeof(tick.TradingDay));
strncpy(tick.UpdateTime,it->UpdateTime, sizeof(tick.UpdateTime));
data.insert(tick);
}
datas_tick.datas.clear();
}
data.write(a.path_str + "/bars/"+(*n_p), 1000000);
}
cout<<"完毕"<<endl;
}


//合成全部
if(str == "hb_all")
{
vector<string> path_list, file_list;
map<string, vector<string> > map_list;
set<string> set_list;
string dz;
cout<<"；请输入来源目录地址"<<endl;
cin>>dz;
get_file_list(dz, path_list);
for(auto path: path_list)
{
get_file_list(dz + "/" + path, map_list[path]);
}
for(auto pair: map_list)
{
set_list.insert(pair.second.begin(), pair.second.end() );
} 
cout<<set_list.size()<<endl;

int num = 0, size = set_list.size();
for(auto name: set_list)
{
num++;
class class_bars bars;
for(auto pair: map_list)
{
if(find(pair.second.begin(), pair.second.end(), name) != pair.second.end() )
{
bars.read_tick(dz + "/" + pair.first + "/" + name, 10000000);
}
}
bars.write(a.path_str + "/bars/" + name,10000000);
cout<<num<<"/"<<size<<endl;
}
cout<<"完毕"<<endl;
}


//合成0000指数
if(str == "0000")
{

vector<string> file_list;//文件名列表

cout<<"请输入目录地址"<<endl;
string path="";
cin>>path;

get_file_list(path, file_list);

map<string, set<string> > name_map_set;//分类set表
for(auto &file: file_list)
{
string type="",name="",to="";
if(instrument_to_type(file, type, name, to) == 0)
{
name_map_set[type].insert(name);
}
}
cout<<"分类数量"<<name_map_set.size()<<endl;

for(auto it = name_map_set.begin(); it != name_map_set.end(); ++it)//便利名称分类
{
map<string, class_bars> bars_map;//该分类的行情列表map
map<string, struct_bar> bar_map;//该分类的行情map
class_bars bars0000;//合成指数对象
for(auto itt = it->second.begin(); itt != it->second.end(); ++itt)//便利该分类的每一个名称
{
bars_map[*itt].read_tick(path + "/" +*itt+".dat",1000000);
bar_map[*itt]= *bars_map[*itt].datas.begin();
}

auto_date_time adt;//自动日期时间对象
string date_old="", date= bar_map.begin()->second.date, time= bar_map.begin()->second.time;//日期，时间
adt.init(date,time);//初始化日期时间对象
for(int i=0;i<7000;++i)//逐分钟便利
{
if(date != date_old)//新的一天
{
date_old = date;
bar_map.clear();
for(auto ittt=bars_map.begin(); ittt !=bars_map.end(); ++ittt)
{
auto itttdt = find_if(ittt->second.datas.begin(), ittt->second.datas.end(), [&](const struct_bar &x){return x.date == date;});
if(itttdt != ittt->second.datas.end() )
{
bar_map[ittt->first] = *itttdt;
}
}
}

for(auto itit=bar_map.begin(); itit != bar_map.end(); ++itit)
{
auto ititdt = find_if(bars_map[itit->first].datas.begin(), bars_map[itit->first].datas.end(), [&](const struct_bar x){ return x.date == date && x.time == time;});   
if(ititdt != bars_map[itit->first].datas.end() )//更新最新bar
{
bar_map[itit->first] = *ititdt; 
}
}

if( find_if(bar_map.begin(),bar_map.end(), [&](const pair<string, struct_bar> &x){ return x.second.date == date && x.second.time == time;}) != bar_map.end() )//如果有任何一个更新则执行下面的
{
i=0;
struct_bar bar0000;
memset(&bar0000, 0, sizeof(struct_bar) );
bar0000.open_interest = accumulate(bar_map.begin(), bar_map.end(), 0, [](int a, const pair<string, struct_bar> &b){return a+b.second.open_interest;});
bar0000.volume = accumulate(bar_map.begin(), bar_map.end(), 0, [](int a, const pair<string, struct_bar> &b){return a+b.second.volume;});
bar0000.o = accumulate(bar_map.begin(), bar_map.end(), 0.0, [](double a, const pair<string, struct_bar> &b){return a+b.second.o * b.second.open_interest;}) / bar0000.open_interest;
bar0000.h = accumulate(bar_map.begin(), bar_map.end(), 0.0, [](double a, const pair<string, struct_bar> &b){return a+b.second.h * b.second.open_interest;}) / bar0000.open_interest;
bar0000.l = accumulate(bar_map.begin(), bar_map.end(), 0.0, [](double a, const pair<string, struct_bar> &b){return a+b.second.l * b.second.open_interest;}) / bar0000.open_interest;
bar0000.c = accumulate(bar_map.begin(), bar_map.end(), 0.0, [](double a, const pair<string, struct_bar> &b){return a+b.second.c * b.second.open_interest;}) / bar0000.open_interest;
strncpy(bar0000.date, date.c_str(), sizeof(bar0000.date));
strncpy(bar0000.time, time.c_str(), sizeof(bar0000.time) );
bars0000.datas.push_back(bar0000);
}
adt.get_next(date,time);//更新日期时间
}
bars0000.write_txt(a.path_str + "/0000/"+it->first +".txt",1000000);
}

cout<<"完毕"<<endl;
}


//ticks推送
if(str == "ticks_push")
{
string name;
cout<<"请输入tick地址"<<endl;
cin>>name;
class_tick datas_tick;
cout<<"读入"<<datas_tick.read(name,1000000)<<endl;
CThostFtdcDepthMarketDataField tick;
memset(&tick, 0, sizeof(CThostFtdcDepthMarketDataField) );
for(auto it=datas_tick.datas.begin();it != datas_tick.datas.end();++it)
{
tick.LastPrice=it->LastPrice;
tick.BidPrice1=it->BidPrice1;
tick.AskPrice1=it->AskPrice1;
tick.OpenInterest=it->OpenInterest;
tick.Volume=it->Volume;
tick.BidVolume1=it->BidVolume1;
tick.AskVolume1=it->AskVolume1;
strncpy(tick.TradingDay,it->TradingDay, sizeof(tick.TradingDay));
strncpy(tick.UpdateTime,it->UpdateTime, sizeof(tick.UpdateTime));
a.tick_map[name]=tick;
a.flag[35]=1;
//脉冲
if(a.tick_event_map.find(name) != a.tick_event_map.end())
{
a.tick_event_map[name].notify();
}

}
cout<<"完毕"<<endl;
}


//状态
if (str == "status")
{
cout<<"行情api版本:" << a.md_api_p->GetApiVersion()<<endl<<"交易api版本:" << a.trader_api_p->GetApiVersion()<<endl;
cout<<"编译日期:"<< __DATE__<<",时间:" << __TIME__ << endl;
//输出
cout<<"启动时间:"<<now_tm.tm_hour<<":"<<now_tm.tm_min<<":"<< now_tm.tm_sec<< " "<< std::chrono::duration_cast<std::chrono::milliseconds>(now_0.time_since_epoch()).count() % 1000 << " "<< 1900 + now_tm.tm_year<< "-"<< 1 + now_tm.tm_mon<< "-"<< now_tm.tm_mday<<endl;
cout<<"启动位置:"<<std::filesystem::current_path()<<",项目:"<<argv[0]<<endl;
for (size_t i = 0; i < 64; ++i)
{
if (a.flag[i] != 0)
{
cout << i << ":" << a.flag[i] << ",";
}
}
cout<<endl;

cout << "broker:" << a.broker_str << ",user:" << a.user_str ;
cout << ",目录:"<<a.path_str<<",交易日:"<<a.tradingday << endl;

int num=0;
for(auto &eve: a.tick_event_map)
{
num += eve.second.funcs_map.size();
}
cout<<"线程池:" << pool.threads.size() << ",任务数:" << pool.functions.size() <<",合约数量:" << num <<endl;

cout << "操作引用:"<<a.nrequestid<<",前置机:" << a.frontid << ",绘画:" << a.sessionid << ",最大引用:" << a.orderref << endl;
cout<<"tick状态:"<<a.tick_status_map.size()<<",";
int status_2=0;
for(auto it=a.tick_status_map.begin();it !=a.tick_status_map.end();++it)
{
if(it->second == '2')
{
status_2 ++; 
}
}
cout<<"连续交易:"<<status_2<<",";
cout<<"查询合约请求响应:"<<a.hy_map.size()<<",";
cout<<"合约交易状态通知:"<<a.instrument_status_map.size();
cout<<endl;
double turnover = accumulate(a.tick_map.begin(), a.tick_map.end(), 0.0, [](double x, const pair<string, CThostFtdcDepthMarketDataField> &y){return (y.second.Volume > 0) ? x+y.second.Turnover : x;});
double turnover_all = accumulate(a.tick_map.begin(), a.tick_map.end(), 0.0, [](double x, const pair<string, CThostFtdcDepthMarketDataField> &y){double z = y.second.OpenInterest * y.second.LastPrice * a.hy_map[y.first].VolumeMultiple;return std::isnan(z)? x : x + z; });
printf("总成交:%g,总沉淀:%g,占比:%g%%\n", turnover, turnover_all, turnover / turnover_all * 100);

cout<<"合约事件"<<a.tick_event_map.size()<<endl;
for(auto pair = a.tick_event_map.begin(); pair != a.tick_event_map.end();++pair)
{
cout<<pair->first<<",数量:"<<pair->second.funcs_map.size()<<endl;
}
cout << "tick:" << a.tick_map.size() << ",bars:" << a.bars_map.size()<<",ticks:"<<a.ticks_map.size()<< endl;
int sl = 0;
for (auto it = a.bars_map.begin(); it != a.bars_map.end(); ++it)
{
sl += it->second.datas.size();
}
cout << "bar总量:"<<sl ;
sl=0;
for(auto it=a.ticks_map.begin();it !=a.ticks_map.end();++it)
{
sl += it->second.datas.size();
}
cout<<",tick总量:"<<sl<<endl;
cout << "日志:" << a.log.r << ",写出:" << a.log.w << ",分类:"<<a.log.log_map_deque.size()<<endl;
cout<<"海归:"<<cl_turtle.size()
<<",趋势:"<<cl_trend.size()
<<",震荡:"<<cl_wave.size()
<<",价差回归:"<<cl_spread.size()
<<",动能:"<<cl_energy.size()
<<",网格:"<<cl_grid.size()
<<",网格静态:"<<cl_grid_static.size()
<<",网格平衡:"<<cl_grid_5.size()
<<",比例:"<<cl_ba.size()
<<",动态跟踪:"<<cl_adaptive.size()
<<endl;

a.position_sync(cccc);
//清理报单返回值
a.return_erase(cccc);

for (auto it = cccc.return_vector.begin(); it != cccc.return_vector.end(); ++it)
{
printf("合约代码:%s,ID:%d,前置机:%d,绘画:%d,报单引用:%s,日期:%d\n", it->name, it->id, it->frontid, it->sessionid, it->orderref, it->tradingday);
}
cout << "手动持仓:" << cccc.position << ",进行中报单:" << cccc.return_vector.size() << endl;
cout<<"完毕"<<endl;
}


//前置机
if(str == "front")
{
cout<<"行情前置机数量:"<<a.md_front_list.size()<<endl;
for(auto &front: a.md_front_list)
{
cout<<front<<endl;
}
cout<<"交易前置机数量:"<<a.trader_front_list.size()<<endl;
for(auto &front: a.trader_front_list)
{
cout<<front<<endl;
}

//获取前置机信息
cout<<"当前前置机信息:"<<endl;
CThostFtdcFrontInfoField front_info;
memset(&front_info, 0, sizeof(	CThostFtdcFrontInfoField));
a.trader_api_p->GetFrontInfo(&front_info);
cout<<"前置地址"
<<front_info.FrontAddr
<<",查询流控"
<<front_info.QryFreq
<<",FTD流控"
<<front_info.FTDPkgFreq
<<endl;
cout<<"完毕"<<endl;
}


//假日
if(str == "holiday")
{
cout<<"假日数量:"<<a.holiday_list.size()<<endl;
for(auto &holiday: a.holiday_list)
{
cout<<holiday<<endl;
}
// 获取当前时间点
auto now_0 = std::chrono::system_clock::now();
// 获取当前时间点的年月日时分秒毫秒
std::time_t now_c = std::chrono::system_clock::to_time_t(now_0);
tm now_tm = *std::localtime(&now_c);  
char date[9] = {0};
strftime(date, sizeof(date), "%Y%m%d", &now_tm);

if(find(a.holiday_list.begin(), a.holiday_list.end(), date) != a.holiday_list.end() )
{
cout<<"今天"<<date<<"放假"<<endl;
}
else
{
cout<<"今天"<<date<<"不放假"<<endl;
}
cout<<"完毕"<<endl;
}


//策略状态
if (str == "cl")
{

cout<<"海龟数量:"<<cl_turtle.size()<<endl;
for (auto it = cl_turtle.begin(); it != cl_turtle.end(); ++it)
{
cout << (*it)->name << "持仓" << (*it)->cc->position<<"价值"<<((*it)->cc->value + (*it)->cc->position * a.tick_map[(*it)->name].LastPrice) * a.hy_map[(*it)->name].VolumeMultiple;
cout << "均线1=" << (*it)->dous[1] << "均线2=" << (*it)->dous[2] << "均线3=" << (*it)->dous[3] << "多开" << (*it)->dous[4] << "空开" << (*it)->dous[5] << "多平" << (*it)->dous[7] << "空平" << (*it)->dous[6] << "价格"<<(*it)->data.LastPrice<<"时间"<<(*it)->data.UpdateTime<<endl;
if((*it)->cc->return_vector.size() > 0)
{
cout<<"进行中报单"<<(*it)->cc->return_vector.size()<<endl;
}
}
cout<<"趋势数量:"<<cl_trend.size()<<endl;
for (auto it = cl_trend.begin(); it != cl_trend.end(); ++it)
{
cout << (*it)->name << "持仓" << (*it)->cc->position<<"价值"<<((*it)->cc->value + (*it)->cc->position * a.tick_map[(*it)->name].LastPrice) * a.hy_map[(*it)->name].VolumeMultiple;
cout << "周期1高=" << (*it)->dous[1] << "周期2高=" << (*it)->dous[3] << "周期3高=" << (*it)->dous[5] << "周期1低=" << (*it)->dous[2] << "周期2低=" << (*it)->dous[4] << "周期3低=" << (*it)->dous[6] << "价格"<<(*it)->data.LastPrice<<"时间"<<(*it)->data.UpdateTime<<endl;
if((*it)->cc->return_vector.size() > 0)
{
cout<<"进行中报单"<<(*it)->cc->return_vector.size()<<endl;
}
}
cout<<"震荡数量:"<<cl_wave.size()<<endl;
for (auto it = cl_wave.begin(); it != cl_wave.end(); ++it)
{
cout << (*it)->name << "持仓" << (*it)->cc->position<<"价值"<<((*it)->cc->value + (*it)->cc->position * a.tick_map[(*it)->name].LastPrice) * a.hy_map[(*it)->name].VolumeMultiple;
cout << "中轴均线=" << (*it)->dous[7] << "买入价格=" << (*it)->dous[6] << "多头止损=" << (*it)->dous[5] << "卖出价格=" << (*it)->dous[8] << "空头止损=" << (*it)->dous[9] << endl;
if((*it)->cc->return_vector.size() > 0)
{
cout<<"进行中报单"<<(*it)->cc->return_vector.size()<<endl;
}
}
cout<<"回归数量:"<<cl_spread.size()<<endl;
for (auto it = cl_spread.begin(); it != cl_spread.end(); ++it)
{
cout << (*it)->name << ",持仓:" << (*it)->ints[0]<<",cc价值:"<<((*it)->cc->value + (*it)->cc->position * a.tick_map[(*it)->name_a].LastPrice) * a.hy_map[(*it)->name_a].VolumeMultiple<<",dd价值:"<<((*it)->dd->value + (*it)->dd->position * a.tick_map[(*it)->name_b].LastPrice) * a.hy_map[(*it)->name_b].VolumeMultiple;
cout<<",价差:"<<a.tick_map[(*it)->name_a].LastPrice - a.tick_map[(*it)->name_b].LastPrice;
cout << ",均线:" << (*it)->dous[9]<<endl;
if((*it)->cc->position != (*it)->dd->position)
{
cout<<"cc:"<<(*it)->cc->position << ",dd:" << (*it)->dd->position << endl;
}

if((*it)->cc->return_vector.size() > 0)
{
cout<<"cc进行中报单"<<(*it)->cc->return_vector.size()<<endl;
}
if((*it)->dd->return_vector.size() > 0)
{
cout<<"dd进行中报单"<<(*it)->dd->return_vector.size()<<endl;
}
}


cout<<"网格数量:"<<cl_grid.size()<<endl;
for (auto it = cl_grid.begin(); it != cl_grid.end(); ++it)
{
cout << (*it)->name << "持仓" << (*it)->cc->position<<"价值"<<((*it)->cc->value + (*it)->cc->position * a.tick_map[(*it)->name].LastPrice) * a.hy_map[(*it)->name].VolumeMultiple;
cout<<",均线:"<<(*it)->dous[7]<<",间距:"<<(*it)->dous[1]<<",周期:"<<(*it)->ints[1]<<",当前价:"<<(*it)->data.LastPrice<<",时间:"<<(*it)->data.UpdateTime<<endl;
if((*it)->cc->return_vector.size() > 0)
{
cout<<"进行中报单"<<(*it)->cc->return_vector.size()<<endl;
for(auto itt = (*it)->cc->return_vector.begin();itt != (*it)->cc->return_vector.end();++itt)
{
cout<<"id:"<<itt->id<<",frontid:"<<itt->frontid<<",sessionid:"<<itt->sessionid<<",orderref:"<<itt->orderref<<",tradingday:"<<itt->tradingday<<",traded:"<<itt->traded<<endl;
}

}
}


cout<<"网格静态数量:"<<cl_grid_static.size()<<endl;
for (auto it = cl_grid_static.begin(); it != cl_grid_static.end(); ++it)
{
cout << (*it)->name << "持仓" << (*it)->cc->position<<"价值"<<((*it)->cc->value + (*it)->cc->position * a.tick_map[(*it)->name].LastPrice) * a.hy_map[(*it)->name].VolumeMultiple;
cout<<",中线:"<<(*it)->dous[0]<<",间距:"<<(*it)->dous[1]<<",当前价:"<<(*it)->data.LastPrice<<",时间:"<<(*it)->data.UpdateTime<<endl;
if((*it)->cc->return_vector.size() > 0)
{
cout<<"进行中报单"<<(*it)->cc->return_vector.size()<<endl;
for(auto itt = (*it)->cc->return_vector.begin();itt != (*it)->cc->return_vector.end();++itt)
{
cout<<"id:"<<itt->id<<",frontid:"<<itt->frontid<<",sessionid:"<<itt->sessionid<<",orderref:"<<itt->orderref<<",tradingday:"<<itt->tradingday<<",traded:"<<itt->traded<<endl;
}
}
}


cout<<"网格平衡数量:"<<cl_grid_5.size()<<endl;
for (auto it = cl_grid_5.begin(); it != cl_grid_5.end(); ++it)
{
cout << (*it)->name << "持仓" << (*it)->cc->position<<"价值"<<((*it)->cc->value + (*it)->cc->position * a.tick_map[(*it)->name].LastPrice) * a.hy_map[(*it)->name].VolumeMultiple;
cout<<",价格:"<<(*it)->tick.LastPrice<<",时间:"<<(*it)->tick.UpdateTime<<endl;
if((*it)->cc->return_vector.size() > 0)
{
cout<<"进行中报单"<<(*it)->cc->return_vector.size()<<endl;
for(auto itt = (*it)->cc->return_vector.begin();itt != (*it)->cc->return_vector.end();++itt)
{
cout<<"id:"<<itt->id<<",frontid:"<<itt->frontid<<",sessionid:"<<itt->sessionid<<",orderref:"<<itt->orderref<<",tradingday:"<<itt->tradingday<<",traded:"<<itt->traded<<endl;
}

}
}


cout<<"动能数量:"<<cl_energy.size()<<endl;
for (auto it = cl_energy.begin(); it != cl_energy.end(); ++it)
{
cout << (*it)->name << "持仓" << (*it)->cc->position<<"价值"<<((*it)->cc->value + (*it)->cc->position * a.tick_map[(*it)->name].LastPrice) * a.hy_map[(*it)->name].VolumeMultiple;
cout<<",动能:"<<(*it)->dous[8]<<",价格:"<<(*it)->tick.LastPrice<<",时间:"<<(*it)->tick.UpdateTime<<endl;
if((*it)->cc->return_vector.size() > 0)
{
cout<<"进行中报单"<<(*it)->cc->return_vector.size()<<endl;
for(auto itt = (*it)->cc->return_vector.begin();itt != (*it)->cc->return_vector.end();++itt)
{
cout<<"id:"<<itt->id<<",frontid:"<<itt->frontid<<",sessionid:"<<itt->sessionid<<",orderref:"<<itt->orderref<<",tradingday:"<<itt->tradingday<<",traded:"<<itt->traded<<endl;
}

}
}


cout<<"比例数量:"<<cl_ba.size()<<endl;
for (auto it = cl_ba.begin(); it != cl_ba.end(); ++it)
{
cout << (*it)->name << "持仓" << (*it)->cc->position<<"价值"<<((*it)->cc->value + (*it)->cc->position * a.tick_map[(*it)->name].LastPrice) * a.hy_map[(*it)->name].VolumeMultiple;
cout << "比利=" << (*it)->dous[7] << "买入价格=" << (*it)->data.BidPrice1 << ",总数量=" << (*it)->dous[5] << ",卖出价格=" << (*it)->data.AskPrice1 << ",总数量=" << (*it)->dous[6] << endl;
if((*it)->cc->return_vector.size() > 0)
{
cout<<"进行中报单"<<(*it)->cc->return_vector.size()<<endl;
}
}

cout<<"动态跟踪数量:"<<cl_adaptive.size()<<endl;
for (auto it = cl_adaptive.begin(); it != cl_adaptive.end(); ++it)
{
cout << (*it)->name << ",跟踪仓位:" << (*it)->cc->position <<",价值:"<<((*it)->cc->value + (*it)->cc->position * a.tick_map[(*it)->name].LastPrice) * a.hy_map[(*it)->name].VolumeMultiple;
cout<<",关注价格:"<<(*it)->cc->dous[0]<<",价格:"<<(*it)->tick.LastPrice<<",时间:"<<(*it)->tick.UpdateTime<<endl;
if((*it)->cc->return_vector.size() > 0)
{
cout<<"进行中报单"<<(*it)->cc->return_vector.size()<<endl;
for(auto itt = (*it)->cc->return_vector.begin();itt != (*it)->cc->return_vector.end();++itt)
{
cout<<"id:"<<itt->id<<",frontid:"<<itt->frontid<<",sessionid:"<<itt->sessionid<<",orderref:"<<itt->orderref<<",tradingday:"<<itt->tradingday<<",traded:"<<itt->traded<<endl;
}

}
}

cout<<"完毕"<<endl;
}


//全部清仓
if(str == "qc")
{
cout<<"请输入y n 确定是否清仓"<<endl;
string yn;
cin>>yn;
if(yn != "y")
{
cout<<"取消完成"<<endl;
continue;
}
int position=0;
for (auto it = a.position_map.begin(); it != a.position_map.end(); ++it)
{
position=it->second.p12.Position + it->second.p22.Position;
if(position > 0)
{
a.bd_auto((char*)it->first.c_str(),'1',a.tick_map[it->first].LowerLimitPrice,position,cccc);
}
position=it->second.p13.Position + it->second.p23.Position;
if(position > 0)
{
a.bd_auto((char*)it->first.c_str(),'0',a.tick_map[it->first].UpperLimitPrice,position,cccc);
}
}

cout<<"完毕"<<endl;
}


//撤单
if (str == "cd")
{
a.cd(cccc);
cout<<"完毕"<<endl;
}


//清屏
if (str == "cls")
{
printf("\033c");
}


//查询资金
if (str == "zj")
{
double qlj = 0.0;
for(auto& [name, position] : a.position_map)
{
if(a.hy_map[name].OptionsType == '1' || a.hy_map[name].OptionsType == '2' )
{
qlj += ((position.p23.Position - position.p22.Position + position.p13.Position - position.p12.Position) * a.tick_map[name].LastPrice * a.hy_map[name].VolumeMultiple );
}
}
//	 printf("期货结算准备金:%.2f,占用保证金总额:%.2f,特殊产品占用保证金:%.2f\n", a.trading_account.Balance, a.trading_account.CurrMargin, a.trading_account.SpecProductMargin);
//printf("仓位:%.2f%%,期权权利金:%.2f,占比:%.2f%%\n", a.trading_account.CurrMargin / a.trading_account.Balance * 100, qlj, qlj / a.trading_account.Balance * 100);
//printf("可用资金:%.2f,交易所保证金:%.2f,冻结的保证金:%.2f\n", a.trading_account.Available, a.trading_account.ExchangeMargin, a.trading_account.FrozenMargin);

//printf("期货结算准备金:%.2f,占用保证金总额:%.2f,仓位:%.2f\n", a.trading_account.Balance, a.trading_account.CurrMargin, a.trading_account.CurrMargin / a.trading_account.Balance * 100);
//printf("特殊产品占用保证金:%.2f,权利金:%.2f,仓位:%.2f\n", a.trading_account.SpecProductMargin, qlj, a.trading_account.SpecProductMargin / a.trading_account.Balance * 100);
//
//printf("可用资金:%.2f,交易所保证金:%.2f,冻结的保证金:%.2f\n", a.trading_account.Available, a.trading_account.ExchangeMargin, a.trading_account.FrozenMargin);
//str << "经纪公司代码" << a.trading_account.BrokerID << "投资者帐号" << a.trading_account.AccountID << "上次质押金额" << a.trading_account.PreMortgage << "上次信用额度" << a.trading_account.PreCredit << "上次存款额" << a.trading_account.PreDeposit
//<< "利息基数" << a.trading_account.InterestBase << "利息收入" << a.trading_account.Interest
//<< "入金金额" << a.trading_account.Deposit << "出金金额" << a.trading_account.Withdraw
//<< "资金差额" << a.trading_account.CashIn
//<< "基本准备金" << a.trading_account.Reserve << "交易日" << a.trading_account.TradingDay << "结算编号" << a.trading_account.SettlementID << "信用额度" << a.trading_account.Credit << "质押金额" << a.trading_account.Mortgage
//<< "投资者交割保证金" << a.trading_account.DeliveryMargin << "交易所交割保证金" << a.trading_account.ExchangeDeliveryMargin << "保底期货结算准备金" << a.trading_account.ReserveBalance << "币种代码" << a.trading_account.CurrencyID << "上次货币质入金额" << a.trading_account.PreFundMortgageIn << "上次货币质出金额" << a.trading_account.PreFundMortgageOut << "货币质入金额" << a.trading_account.FundMortgageIn << "货币质出金额" << a.trading_account.FundMortgageOut << "货币质押余额" << a.trading_account.FundMortgageAvailable << "可质押货币金额" << a.trading_account.MortgageableFund
printf("上次结算准备金:%.2f,上次占用的保证金:%.2f,占比:%.2f%%\n", a.trading_account.PreBalance, a.trading_account.PreMargin, a.trading_account.PreMargin / a.trading_account.PreBalance * 100);
printf("期货结算准备金:%.2f,当前保证金总额:%.2f,仓位:%.2f%%\n", a.trading_account.Balance, a.trading_account.CurrMargin,a.trading_account.CurrMargin / a.trading_account.Balance* 100);
printf("期权权利金:%.2f,占比:%.2f%%,入金金额:%.2f,出金金额:%.2f\n", qlj, qlj / a.trading_account.Balance * 100,a.trading_account.Deposit, a.trading_account.Withdraw);
printf("可用资金:%.2f,可取资金:%.2f,资金差额:%.2f\n", a.trading_account.Available, a.trading_account.WithdrawQuota, a.trading_account.CashIn);
printf("冻结的保证金:%.2f,冻结的资金:%.2f,冻结的手续费:%.2f\n", a.trading_account.FrozenMargin, a.trading_account.FrozenCash, a.trading_account.FrozenCommission);
printf("手续费:%.2f, 平仓盈亏:%.2f,持仓盈亏:%.2f\n", a.trading_account.Commission, a.trading_account.CloseProfit, a.trading_account.PositionProfit);
printf("交易所保证金:%.2f,占比:%.2f%%\n", a.trading_account.ExchangeMargin, a.trading_account.ExchangeMargin / a.trading_account.Balance * 100);
printf("特殊产品占用保证金:%.2f,特殊产品冻结保证金:%.2f,特殊产品手续费:%.2f,特殊产品冻结手续费:%.2f\n", a.trading_account.SpecProductMargin, a.trading_account.SpecProductFrozenMargin, a.trading_account.SpecProductCommission, a.trading_account.SpecProductFrozenCommission); 
printf("特殊产品交易所保证金:%.2f,特殊产品持仓盈亏:%.2f,特殊产品平仓盈亏:%.2f,根据持仓盈亏算法计算的特殊产品持仓盈亏:%.2f\n", a.trading_account.SpecProductExchangeMargin, a.trading_account.SpecProductPositionProfit, a.trading_account.SpecProductCloseProfit, a.trading_account.SpecProductPositionProfitByAlg);
cout<<"完毕"<<endl;
}


//自选板块
if( strncmp(str.c_str(), "zx", 2) == 0)
{
vector<vector<string> > vs;
vector<CThostFtdcDepthMarketDataField> data_list;
for(auto v: read_csv(a.path_str+"/configuration/"+ str + ".txt"))
{
//cout<<a.zm.zm(a.hy_map[v[0]].InstrumentName)<<",幅度:"<< (a.tick_map[v[0]].LastPrice / a.tick_map[v[0]].PreSettlementPrice - 1) * 100 <<",价格:"<<a.tick_map[v[0]].LastPrice<<",持仓:"<<a.tick_map[v[0]].OpenInterest<<",成交:"<<a.tick_map[v[0]].Volume<<endl;
data_list.push_back(a.tick_map[v[0]] );
}
sort(data_list.begin(),data_list.end(),[](CThostFtdcDepthMarketDataField &a,CThostFtdcDepthMarketDataField &b){return a.LastPrice / a.PreSettlementPrice > b.LastPrice / b.PreSettlementPrice;});
for(auto tick:data_list)
{
//cout<<a.zm.zm(a.hy_map[tick.InstrumentID].InstrumentName)<<",幅度:"<< (tick.LastPrice / tick.PreSettlementPrice - 1) * 100 <<",价格:"<<tick.LastPrice<<",持仓:"<<tick.OpenInterest<<",成交:"<<tick.Volume<<endl;
printf("%s,幅度:%.3f,价格:%.3f,持仓:%.0f,增减:%.0f,成交:%d\n", a.zm.zm(a.hy_map[tick.InstrumentID].InstrumentName).c_str(), (tick.LastPrice / tick.PreSettlementPrice - 1) * 100, tick.LastPrice, tick.OpenInterest, tick.OpenInterest - tick.PreOpenInterest, tick.Volume);
}
cout<<"完毕"<<endl;
}


//自选主力
if( strncmp(str.c_str(), "bk", 2) == 0)
{
vector<vector<string> > vs;
vector<CThostFtdcDepthMarketDataField> data_list;
for(auto v: read_csv(a.path_str+"/configuration/"+ str + ".txt"))
{
//cout<<a.zm.zm(a.hy_map[v[0]].InstrumentName)<<",幅度:"<< (a.tick_map[v[0]].LastPrice / a.tick_map[v[0]].PreSettlementPrice - 1) * 100 <<",价格:"<<a.tick_map[v[0]].LastPrice<<",持仓:"<<a.tick_map[v[0]].OpenInterest<<",成交:"<<a.tick_map[v[0]].Volume<<endl;
if(!a.hot_map_vector[v[0]].empty() )
{
data_list.push_back(a.tick_map[a.hot_map_vector[v[0]].begin()->InstrumentID ] );
}
}
sort(data_list.begin(),data_list.end(),[](CThostFtdcDepthMarketDataField &a,CThostFtdcDepthMarketDataField &b){return a.LastPrice / a.PreSettlementPrice > b.LastPrice / b.PreSettlementPrice;});
for(auto tick:data_list)
{
//cout<<a.zm.zm(a.hy_map[tick.InstrumentID].InstrumentName)<<",幅度:"<< (tick.LastPrice / tick.PreSettlementPrice - 1) * 100 <<",价格:"<<tick.LastPrice<<",持仓:"<<tick.OpenInterest<<",成交:"<<tick.Volume<<endl;
printf("%s,幅度:%.3f,价格:%.3f,持仓:%.0f,增减:%.0f,成交:%d\n", a.zm.zm(a.hy_map[tick.InstrumentID].InstrumentName).c_str(), (tick.LastPrice / tick.PreSettlementPrice - 1) * 100, tick.LastPrice, tick.OpenInterest, tick.OpenInterest - tick.PreOpenInterest, tick.Volume);
}
cout<<"完毕"<<endl;
}


//查询行情
if (str == "hq")
{
cout << "请输入合约代码" << endl;
string dm;
getline(cin, dm,'\n');
CThostFtdcDepthMarketDataField tick = a.tick_map[dm];
auto hy = a.hy_map[dm];
cout << tick.LastPrice <<"开"<<tick.OpenPrice<< "高" << tick.HighestPrice << "涨停"<<tick.UpperLimitPrice<<"低" << tick.LowestPrice << "跌停"<<tick.LowerLimitPrice<<"成交" << tick.Volume << "持仓" << tick.OpenInterest <<",增减:"<<tick.OpenInterest - tick.PreOpenInterest<< endl;

cout << "买1b:" << tick.BidPrice1 << ",v:" << tick.BidVolume1 << ",卖1a:" << tick.AskPrice1 << ",v:" << tick.AskVolume1 << ",比例:" << (double)tick.BidVolume1 / (double)(tick.BidVolume1 + tick.AskVolume1) << ",换手:" << tick.Volume / tick.OpenInterest * 100 <<"%" <<endl;
if( tick.BidVolume2 > 0 || tick.AskVolume2 > 0 )
{
cout << "买2b:" << tick.BidPrice2 << ",v:" << tick.BidVolume2 << ",卖2a:" << tick.AskPrice2 << ",v:" << tick.AskVolume2 << endl;
}
if( tick.BidVolume3 > 0 || tick.AskVolume3 > 0 )
{
cout << "买3b:" << tick.BidPrice3 << ",v:" << tick.BidVolume3 << ",卖3a:" << tick.AskPrice3 << ",v:" << tick.AskVolume3 << endl;
}
if( tick.BidVolume4 > 0 || tick.AskVolume4 > 0 )
{
cout << "买4b:" << tick.BidPrice4 << ",v:" << tick.BidVolume4 << ",卖4a:" << tick.AskPrice4 << ",v:" << tick.AskVolume4 << endl;
}
if( tick.BidVolume5 > 0 || tick.AskVolume5 > 0 )
{
cout << "买5b:" << tick.BidPrice5 << ",v:" << tick.BidVolume5 << ",卖5a:" << tick.AskPrice5 << ",v:" << tick.AskVolume5 << endl;
}

cout << tick.UpdateTime << " " << tick.UpdateMillisec << " " << tick.TradingDay << tick.ActionDay << endl;
//cout << a.zm.zm(hy.InstrumentName)<<"涨幅"<<(tick.LastPrice / tick.PreSettlementPrice) * 100 - 100 <<"合约乘数"<<hy.VolumeMultiple<<"最小波动"<<hy.PriceTick<<endl;
printf("%s,涨幅:%.3f%%,合约乘数:%d,最小波动:%.3f\n", a.zm.zm(hy.InstrumentName).c_str(), (tick.LastPrice / tick.PreSettlementPrice) * 100 - 100, hy.VolumeMultiple, hy.PriceTick);
cout<<"开始交割日:"<<hy.StartDelivDate<<",结束交割日:"<<hy.EndDelivDate;
int date_num = date_between(a.tradingday, hy.EndDelivDate );
cout<<",剩余天数:"<<date_num;
double iv = 0.0;
switch(hy.OptionsType)
{
case '1' :
if(hy.ProductClass == '2')
{
iv = impliedVolatility(a.tick_map[hy.UnderlyingInstrID].LastPrice, hy.StrikePrice, date_num, tick.LastPrice, true);
}
printf("\n认购期权隐波:%.3f%%,执行价:%.0f", iv * 100, hy.StrikePrice);
cout<<",日军权利金:"<<tick.LastPrice / date_num * hy.VolumeMultiple;
break;
case '2' :
if(hy.ProductClass == '2')
{
iv = impliedVolatility(a.tick_map[hy.UnderlyingInstrID].LastPrice, hy.StrikePrice, date_num, tick.LastPrice, false);
}
printf("\n认沽期权隐波:%.3f%%,执行价:%.0f", iv * 100, hy.StrikePrice);
cout<<",日军权利金:"<<tick.LastPrice / date_num * hy.VolumeMultiple;
break;
default :
printf(",多头保证金率:%.2f,空头保证金率:%.2f", hy.LongMarginRatio, hy.ShortMarginRatio);
}
cout<<endl;
cout<<"上次结算价:"<<tick.PreSettlementPrice<<",今收盘:"<<tick.ClosePrice<<",本次结算价:"<<tick.SettlementPrice<<",当日均价:"<<tick.AveragePrice<<endl;
if(a.position_map.count(dm) > 0)
{
if (a.position_map[dm].p12.Position != 0 || a.position_map[dm].p12.LongFrozen != 0 || a.position_map[dm].p12.ShortFrozen != 0 || a.position_map[dm].p13.Position != 0 || a.position_map[dm].p13.LongFrozen != 0 || a.position_map[dm].p13.ShortFrozen != 0 || a.position_map[dm].p22.Position != 0 || a.position_map[dm].p22.LongFrozen != 0 || a.position_map[dm].p22.ShortFrozen != 0 || a.position_map[dm].p23.Position != 0 || a.position_map[dm].p23.LongFrozen != 0 || a.position_map[dm].p23.ShortFrozen != 0)
{
printf("%s,当前价:%.2f", dm.c_str(), nomax(a.tick_map[dm].LastPrice));
}
if (a.position_map[dm].p12.Position != 0 || a.position_map[dm].p12.LongFrozen != 0 || a.position_map[dm].p12.ShortFrozen != 0)
{
printf("金仓多:%d,多头冻结:%d,空头冻结:%d", a.position_map[dm].p12.Position, a.position_map[dm].p12.LongFrozen,a.position_map[dm].p12.ShortFrozen);
}
if (a.position_map[dm].p13.Position != 0 || a.position_map[dm].p13.LongFrozen != 0 || a.position_map[dm].p13.ShortFrozen != 0)
{
printf("金仓空:%d,多头冻结:%d,空头冻结:%d", a.position_map[dm].p13.Position, a.position_map[dm].p13.LongFrozen, a.position_map[dm].p13.ShortFrozen);
}
cout<<endl;
if (a.position_map[dm].p22.Position != 0 || a.position_map[dm].p22.LongFrozen != 0 || a.position_map[dm].p22.ShortFrozen != 0)
{
printf("昨仓多:%d,多头冻结:%d,空头冻结:%d", a.position_map[dm].p22.Position, a.position_map[dm].p22.LongFrozen, a.position_map[dm].p22.ShortFrozen);
}
if (a.position_map[dm].p23.Position != 0 || a.position_map[dm].p23.LongFrozen != 0 || a.position_map[dm].p23.ShortFrozen != 0)
{
printf("昨仓空:%d,多头冻结:%d,空头冻结:%d", a.position_map[dm].p23.Position,  a.position_map[dm].p23.LongFrozen, a.position_map[dm].p23.ShortFrozen);
}
}
cout<<endl;
cout<<"完毕"<<endl;
}


//查询强度
if(str == "qd")
{
cout<<"请输入代码"<<endl;
string dm="";
cin>>dm;

class_bars data;
data.read(a.path_str+"/bars/"+dm+".dat",1000000);
if(a.bars_map.find(dm) != a.bars_map.end() )
{
data.datas.insert(data.datas.end(),a.bars_map[dm].datas.begin(),a.bars_map[dm].datas.end() );
}
else
{
cout<<"即时行情没找到该合约"<<endl;
}

int len = 9000;
if(len <= data.datas.size() )
{
cout<<data.average(0, len)<<","<<data.average(0,len / 3 * 2)<<","<<data.average(0,len / 3)<<endl;	
}
else
{
cout<<"bar数量只有:"<<data.datas.size()<<endl;
}
data.datas.clear();
cout<<"完毕"<<endl;
}


//查询产品数据
if(str == "cps")
{
cout<<"请输入产品id"<<endl;
string cp;
cin>>cp;
vector<CThostFtdcDepthMarketDataField> ticks; 
for(auto pair: a.hy_map)
{
if(pair.second.ProductID == cp)
{
ticks.push_back(a.tick_map[pair.first]);
}
}
double pre_open_interest = accumulate(ticks.begin(), ticks.end(), 0.0, [](double x, auto &y){return x + y.PreOpenInterest;});
double open_interest = accumulate(ticks.begin(), ticks.end(), 0.0, [](double x, auto &y){return x + y.OpenInterest;});
printf("%s合约个数:%zu,总持仓:%.0f,增减:%.0f\n", cp.c_str(), ticks.size(), open_interest, open_interest - pre_open_interest);
cout<<"完毕"<<endl;
}


//批量查询
if(str == "hqs")
{
set<string> names;
string name;
map<double, string> namec;
map<double, string> namep;

cout<<"代码"<<endl;
cin>>name;
for(auto &hy_pair: a.hy_map)
{
if(hy_pair.second.UnderlyingInstrID == name)
{
switch(hy_pair.second.OptionsType)
{
case '1':
namec[hy_pair.second.StrikePrice] = hy_pair.first;
break;
case '2': 
namep[hy_pair.second.StrikePrice] = hy_pair.first;
break;
default :
names.insert(hy_pair.first);
}
}
}

if(namec.size() > 0)
{
for(auto name:namec)
{
cout<<name.second<<",价格:"<<a.tick_map[name.second].LastPrice<<",持仓:"<<a.tick_map[name.second].OpenInterest<<",成交量:"<<a.tick_map[name.second].Volume<<",结束交割日:"<<a.hy_map[name.second].EndDelivDate<<",价值:"<<a.tick_map[name.second].LastPrice * a.tick_map[name.second].OpenInterest<<endl;
}
for(auto name:namep)
{
cout<<name.second<<",价格:"<<a.tick_map[name.second].LastPrice<<",持仓:"<<a.tick_map[name.second].OpenInterest<<",成交量:"<<a.tick_map[name.second].Volume<<",结束交割日:"<<a.hy_map[name.second].EndDelivDate<<",价值:"<<a.tick_map[name.second].LastPrice * a.tick_map[name.second].OpenInterest<<endl;
}
int c = accumulate(namec.begin(), namec.end(),0, [](int x, auto &y){return x + a.tick_map[y.second].OpenInterest;}); 
int p = accumulate(namep.begin(), namep.end(),0, [](int x, auto &y){return x + a.tick_map[y.second].OpenInterest;}); 
cout<<"C:" << c <<",P:"<<p<<",p/c:"<<static_cast<double>(p) / c<<std::endl;
int cv = accumulate(namec.begin(), namec.end(),0, [](int x, auto &y){return x + a.tick_map[y.second].OpenInterest * y.first;}); 
int pv = accumulate(namep.begin(), namep.end(),0, [](int x, auto &y){return x + a.tick_map[y.second].OpenInterest * y.first;}); 
cout<<"C行权平均:"<< static_cast<double>(cv) / c << "P行权平均:" << static_cast<double>(pv) / p <<endl;
int cp = accumulate(namec.begin(), namec.end(),0, [](int x, auto &y){return x + a.tick_map[y.second].OpenInterest * a.tick_map[y.second].LastPrice;}); 
int pp = accumulate(namep.begin(), namep.end(),0, [](int x, auto &y){return x + a.tick_map[y.second].OpenInterest * a.tick_map[y.second].LastPrice;}); 
cout<<"C价值:"<< cp << "P价值:" << pp <<endl;
struct data
{
double a, b, c, d, e;
};
vector<data> datas;
for(auto& [price, xx] : namec)
{
struct data data;
data.a = price;
data.b = accumulate(namec.begin(), namec.end(), 0.0, [&](double x, auto& y){return x + (price < y.first ? a.tick_map[y.second].LastPrice * a.tick_map[y.second].OpenInterest: 0.0 );}); 
data.c = accumulate(namec.begin(), namec.end(), 0.0, [&](double x, auto& y){return x + (price > y.first ?(price - y.first) * a.tick_map[y.second].OpenInterest : 0.0);}); 
data.d = accumulate(namep.begin(), namep.end(), 0.0, [&](double x, auto& y){return x + (price > y.first ? a.tick_map[y.second].LastPrice * a.tick_map[y.second].OpenInterest: 0.0 );}); 
data.e = accumulate(namep.begin(), namep.end(), 0.0, [&](double x, auto& y){return x + (price < y.first ?( y.first - price) * a.tick_map[y.second].OpenInterest : 0.0);}); 
datas.push_back(data);
} 
for(auto& data : datas)
{
printf("执行价:%.2f,认购义务:%.2f,认购权力:%.2f,认沽义务:%.2f,认沽权力:%.2f,总义务:%.2f,总权力:%.2f\n", data.a,data.b, data.c, data.d, data.e, data.b + data.d, data.c + data.e);
}

}
else
{
for(auto name:names)
{
cout<<name<<",价格:"<<a.tick_map[name].LastPrice<<",持仓:"<<a.tick_map[name].OpenInterest<<",成交量:"<<a.tick_map[name].Volume<<",结束交割日:"<<a.hy_map[name].EndDelivDate<<endl;
}
}
cout<<"数量:"<<names.size() + namec.size() + namep.size()<<endl;
cout<<"完毕"<<endl;
}


//末日期权
if(str == "mrqq")
{
set<string> names;

for(auto hy_pair: a.hy_map)
{
if(hy_pair.second.EndDelivDate == a.tradingday)
{
names.insert(hy_pair.first);
}
}
for(auto name:names)
{
cout<<name<<",价格:"<<a.tick_map[name].LastPrice<<",持仓:"<<a.tick_map[name].OpenInterest<<",成交量:"<<a.tick_map[name].Volume<<",结束交割日:"<<a.hy_map[name].EndDelivDate<<endl;
}
cout<<"数量:"<<names.size()<<endl;
cout<<"完毕"<<endl;
}


//查询合约
if (str == "hy")
{
cout << "请输入合约代码" << endl;
string dm;
getline(cin, dm,'\n');
if(a.hy_map.find(dm) == a.hy_map.end() )
{
cout<<"没有该合约!"<<endl;
continue;
}
auto hy = a.hy_map[dm];
cout<<"合约代码"<<hy.InstrumentID<<"交易所代码"<<hy.ExchangeID<<"合约名称"<<a.zm.zm(hy.InstrumentName)<<"合约在交易所的代码"<<hy.ExchangeInstID<<"产品代码"<<hy.ProductID<<"产品类型"<<hy.ProductClass<<endl;
cout<<"交割年份"<<hy.DeliveryYear<<"交割月"<<hy.DeliveryMonth<<"市价单最大下单量"<<hy.MaxMarketOrderVolume<<"市价单最小下单量"<<hy.MinMarketOrderVolume<<"限价单最大下单量"<<hy.MaxLimitOrderVolume<<"限价单最小下单量"<<hy.MinLimitOrderVolume<<"合约数量乘数"<<hy.VolumeMultiple<<"最小变动价位"<<hy.PriceTick<<endl;
cout<<"创建日"<<hy.CreateDate<<"上市日"<<hy.OpenDate<<"到期日"<<hy.ExpireDate<<"开始交割日"<<hy.StartDelivDate<<"结束交割日"<<hy.EndDelivDate<<"合约生命周期状态"<<hy.InstLifePhase<<"当前是否交易"<<hy.IsTrading<<endl;
cout<<"持仓类型"<<hy.PositionType<<"持仓日期类型"<<hy.PositionDateType<<"多头保证金率"<<hy.LongMarginRatio<<"空头保证金率"<<hy.ShortMarginRatio<<"是否使用大额单边保证金算法"<<hy.MaxMarginSideAlgorithm<<endl;
cout<<"基础商品代码"<<hy.UnderlyingInstrID<<"执行价"<<hy.StrikePrice<<"期权类型"<<hy.OptionsType<<"合约基础商品乘数"<<hy.UnderlyingMultiple<<"组合类型"<<hy.CombinationType<<endl;

cout<<"完毕"<<endl;
}


//结算单
if(str == "jsd")
{
cout<<"请求查询投资者结算结果响应:\n"<<a.zm.zm(a.SettlementInfo_str)<<endl;
cout<<"完毕"<<endl;
}


//报单价差
if(str == "bd_jc")
{
TThostFtdcInstrumentIDType dm1,dm2 = "";
cout<<"请分别输入两个合约代码"<<endl;
cin>> dm1>>dm2;
cout<<a.tick_map[dm1].LastPrice - a.tick_map[dm2].LastPrice<<endl;
cout<<"买入:"<<a.tick_map[dm1].AskPrice1 - a.tick_map[dm2].BidPrice1<<",卖出:"<<a.tick_map[dm1].BidPrice1 - a.tick_map[dm2].AskPrice1<<endl;
cout<<"报单请输入 buy or sell"<<endl;
string bs;
cin>>bs;
if(bs == "buy")
{
a.bd_auto(dm1,'0',a.tick_map[dm1].UpperLimitPrice,1,cccc);
a.bd_auto(dm2,'1',a.tick_map[dm2].LowerLimitPrice,1,cccc);
}
if(bs == "sell")
{
a.bd_auto(dm1,'1',a.tick_map[dm1].LowerLimitPrice,1,cccc);
a.bd_auto(dm2,'0',a.tick_map[dm2].UpperLimitPrice,1,cccc);
}
cout<<"完毕"<<endl;
}


//双买卖
if(str == "bd_2")
{
TThostFtdcInstrumentIDType dm1 = "",dm2 = "";
cout<<"请分别输入两个合约代码"<<endl;
cin>> dm1>>dm2;
cout<<a.tick_map[dm1].LastPrice + a.tick_map[dm2].LastPrice<<endl;
cout<<"买入:"<<a.tick_map[dm1].AskPrice1 + a.tick_map[dm2].AskPrice1<<",卖出:"<<a.tick_map[dm1].BidPrice1 + a.tick_map[dm2].BidPrice1<<endl;
cout<<"报单请输入 buy or sell"<<endl;
string bs;
cin>>bs;
if(bs == "buy")
{
a.bd_auto(dm1,'0',a.tick_map[dm1].UpperLimitPrice,1,cccc);
a.bd_auto(dm2,'0',a.tick_map[dm2].UpperLimitPrice,1,cccc);
}
if(bs == "sell")
{
a.bd_auto(dm1,'1',a.tick_map[dm1].LowerLimitPrice,1,cccc);
a.bd_auto(dm2,'1',a.tick_map[dm2].LowerLimitPrice,1,cccc);
}
cout<<"完毕"<<endl;
}


//持仓
if (str == "cc")
{
double price = 0.0;//价值
double value = 0.0;//总价值
for (auto it = a.position_map.begin(); it != a.position_map.end(); ++it)
{
price = (it->second.value + it->second.p12.Position * a.tick_map[it->first].BidPrice1 + it->second.p22.Position * a.tick_map[it->first].BidPrice1 - it->second.p13.Position * a.tick_map[it->first].AskPrice1 - it->second.p23.Position * a.tick_map[it->first].AskPrice1) * a.hy_map[it->first].VolumeMultiple;
value += price;
if (it->second.p12.Position != 0 || it->second.p12.LongFrozen != 0 || it->second.p12.ShortFrozen != 0 || it->second.p13.Position != 0 || it->second.p13.LongFrozen != 0 || it->second.p13.ShortFrozen != 0 || it->second.p22.Position != 0 || it->second.p22.LongFrozen != 0 || it->second.p22.ShortFrozen != 0 || it->second.p23.Position != 0 || it->second.p23.LongFrozen != 0 || it->second.p23.ShortFrozen != 0)
{
cout << endl << it->first <<",价格:"<<a.tick_map[it->first].LastPrice<<",价值:"<<price;
cout<<",剩余天数:"<<date_between(a.tradingday, a.hy_map[it->first].EndDelivDate );
}
if (it->second.p12.Position != 0 || it->second.p12.LongFrozen != 0 || it->second.p12.ShortFrozen != 0)
{
cout << ",金仓多:" << it->second.p12.Position <<",多头冻结:"<<it->second.p12.LongFrozen<<",空头冻结:"<<it->second.p12.ShortFrozen;
}
if (it->second.p13.Position != 0 || it->second.p13.LongFrozen != 0 || it->second.p13.ShortFrozen != 0)
{
cout << ",今仓空:" << it->second.p13.Position << ",多头冻结:" << it->second.p13.LongFrozen << ",空头冻结:" << it->second.p13.ShortFrozen;
}
if (it->second.p22.Position != 0 || it->second.p22.LongFrozen != 0 || it->second.p22.ShortFrozen != 0)
{
cout << ",昨仓多:" << it->second.p22.Position << ",多头冻结:" << it->second.p22.LongFrozen << ",空头冻结:" << it->second.p22.ShortFrozen;
}
if (it->second.p23.Position != 0 || it->second.p23.LongFrozen != 0 || it->second.p23.ShortFrozen != 0)
{
cout << ",昨仓空:" << it->second.p23.Position << ",多头冻结:" << it->second.p23.LongFrozen << ",空头冻结:" << it->second.p23.ShortFrozen;
}
}
printf("\n总价值:%.3f\n", value);
cout<<"完毕"<<endl;
}


//初始化持仓价值
if(str == "cc0")
{
for(auto &pair: a.position_map)
{
//		pair.second.value = (pair.second.p13.Position + pair.second.p23.Position - pair.second.p12.Position - pair.second.p22.Position) * a.tick_map[pair.first].LastPrice;
cout<<pair.first<<"仓位:"<<pair.second.p22.Position - pair.second.p23.Position + pair.second.p12.Position - pair.second.p13.Position<<",当前价值:"<<pair.second.value<<",请输入新的价值:"<<endl;
double value = 0.0;
cin>>value;
if(cin.fail())
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
else
{
pair.second.value = value;
}
}
cout<<"完毕"<<endl;
}


//报单
if (str == "bd")
{
TThostFtdcInstrumentIDType hy = "";
TThostFtdcDirectionType dk = '0';
TThostFtdcPriceType price = 0;
TThostFtdcVolumeType value = 0;
cout << "报单，请输入合约代码" << std::endl;
cin.getline(hy,sizeof(TThostFtdcInstrumentIDType));
cin.ignore();
cout << "请输入多空方向" << std::endl;
cin >> dk;
cin.ignore();
cout << "请输入价格" << std::endl;
cin >> price;
cin.ignore();
cout << "请输入数量" << std::endl;
cin >> value;
cin.ignore();
a.bd_auto(hy, dk, price, value, cccc);
cout<<"完毕"<<endl;
}


//查询均线
if(str == "average")
{
cout<<"请分别输入代码和长度"<<endl;
string dm="";
int len=0;
cin>>dm>>len;

class_bars data;
data.read(a.path_str+"/bars/"+dm+".dat",1000000);
if(a.bars_map.find(dm) != a.bars_map.end() )
{
data.datas.insert(data.datas.end(),a.bars_map[dm].datas.begin(),a.bars_map[dm].datas.end() );
}
else
{
cout<<"即时行情没找到该合约"<<endl;
}

if(len <= data.datas.size() )
{
cout<<data.average(0, len)<<endl;	
}
else
{
cout<<"bar数量只有:"<<data.datas.size()<<endl;
}
data.datas.clear();
cout<<"完毕"<<endl;
}


//查询最低
if(str == "min")
{
cout<<"请分别输入代码和长度"<<endl;
string dm="";
int len=0;
cin>>dm>>len;

class_bars data;
data.read(a.path_str+"/bars/"+dm+".dat",1000000);
if(a.bars_map.find(dm) != a.bars_map.end() )
{
data.datas.insert(data.datas.end(),a.bars_map[dm].datas.begin(),a.bars_map[dm].datas.end() );
}
else
{
cout<<"即时行情没找到该合约"<<endl;
}

if(len <= data.datas.size() )
{
cout<<data.lowest(0,len)<<endl;
}
else
{
cout<<"bar数量只有:"<<data.datas.size()<<endl;
}
data.datas.clear();
cout<<"完毕"<<endl;
}


//查询最高
if(str == "max")
{
cout<<"请分别输入代码和长度"<<endl;
string dm="";
int len=0;
cin>>dm>>len;

class_bars data;
data.read(a.path_str+"/bars/"+dm+".dat",1000000);
if(a.bars_map.find(dm) != a.bars_map.end() )
{
data.datas.insert(data.datas.end(),a.bars_map[dm].datas.begin(),a.bars_map[dm].datas.end() );
}
else
{
cout<<"即时行情没找到该合约"<<endl;
}

if(len <= data.datas.size() )
{
cout << data.highest(0,len) <<endl;	
}
else
{
cout<<"bar数量只有:"<<data.datas.size()<<endl;
}
data.datas.clear();
cout<<"完毕"<<endl;
}


//查询波动率
if(str == "bdl")
{
string dm = "";
int n = 0, ts = 345;
cout<<"请输入合约代码 区间总数 日内bar数字"<<endl;
cin>>dm>>n>>ts;
class class_bars bars;
bars.read(a.path_str + "/bars/"+dm +".dat",10000000);
if(a.bars_map.find(dm) != a.bars_map.end() )
{
bars.datas.insert(bars.datas.end(),a.bars_map[dm].datas.begin(),a.bars_map[dm].datas.end() );
}
else
{
cout<<"实时行情没找到该合约"<<endl;
}
double bdl = bars.bdl(0, n, ts);
printf("进%i天年画波动率:%.4f%%\n", n / ts, bdl * 100);
cout<<"完毕"<<endl;
}


//查询标准差
if(str == "bzc")
{
cout<<"请分别输入代码和长度"<<endl;
string dm="";
int len=0;
cin>>dm>>len;

class_bars data;
data.read(a.path_str+"/bars/"+dm+".dat",1000000);
if(a.bars_map.find(dm) != a.bars_map.end() )
{
data.datas.insert(data.datas.end(),a.bars_map[dm].datas.begin(),a.bars_map[dm].datas.end() );
}
else
{
cout<<"即时行情没找到该合约"<<endl;
}

if(len <= data.datas.size() )
{
cout<<data.sigma(0,len)<<endl;
}
else
{
cout<<"bar数量只有:"<<data.datas.size()<<endl;
}
data.datas.clear();
cout<<"完毕"<<endl;
}


//期权竞价
if(str == "qqdj")
{
double S = 0, K = 0, T = 0, R = 0, sigma = 0;
bool cp = true;
cout<<"请一次输入 标的价格， 行权价， 天数， 无风险收益率， 波动率 以及是否看涨"<<endl;
cin >> S>>K>>T>>R>>sigma>>cp;
double z = blackScholesPrice(S, K, T / 365, R, sigma, cp);
if(cp == true)
{
printf("认购期权定价:%.3f\n", z);
}
else
{
printf("认沽期权定价:%.3f\n", z);
}
cout<<"完毕"<<endl;
}



cin.clear();
cin.sync();
}

if(a.flag[3] != 0)
{
a.log.msg(get_date_time() + "\n" + a.user_str + "\n程序退出");
}
a.log.insert("log","退出主函数");
return 0;
}
